在Asp.Net MVC 中配置 Serilog
Serilog 是一种非常简便记录log 的处理方式,使用Serilog可以生成本地的text文件, 也可以通过 Seq 来在Web界面中查看具体的log内容。
接下来就简单的介绍一下在Asp.Net MVC中如何配置是Serilog 生效:
1):下载并且安装Seq,具体的下载URL 为 【http://getseq.net/Download】,安装到默认的路径之后,实际上时候启动了一个Win Service,并且监听的端口号默认为 5341.
安装的最后一步截图如下:

然后我们到Service列表中可以找到对应的Service, 如下图所示:

2):创建一个Asp.Net MVC 5的一个工程, 然后通过 Nuget 下载并且安装 对应的 package,如下图所示

3):在 App_Start 文件夹下创建一个 class 叫做 SerilogConfig.cs , 代码如下所示
using Serilog;
using SerilogWeb.Classic.Enrichers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Hosting; namespace TestSerilog.App_Start
{
public class SerilogConfig
{
public static ILogger CreateLogger()
{
var logpath = HostingEnvironment.MapPath("~");
var config = new LoggerConfiguration()
.Enrich.WithMachineName()
.Enrich.WithProperty("ApplicationName", AssemblyTitle)
.Enrich.With<HttpRequestClientHostIPEnricher>()
.Enrich.With<HttpRequestRawUrlEnricher>()
.Enrich.With<HttpRequestIdEnricher>()
.Enrich.With<UserNameEnricher>()
//.Enrich.WithProperty("RuntimeVersion", Environment.Version)
// this ensures that calls to LogContext.PushProperty will cause the logger to be enriched
.Enrich.FromLogContext()
.MinimumLevel.Verbose()
.WriteTo.Seq(ConfigurationManager.AppSettings["SeqServer"], apiKey: ConfigurationManager.AppSettings["SeqApiKey"])
.WriteTo.RollingFile(Path.Combine(logpath, "Logs\\EricSunTestLog-{Date}.log"), retainedFileCountLimit: null, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {SourceContext} - ({MachineName}|{HttpRequestId}|{UserName}) {Message}{NewLine}{Exception}");
return config.CreateLogger();
} public static string AssemblyTitle
{
get
{
var attributes = typeof(SerilogConfig).Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > )
{
var titleAttribute = (AssemblyTitleAttribute)attributes[];
if (titleAttribute.Title.Length > )
return titleAttribute.Title;
}
return Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase);
}
}
}
}
4):在 Web.config 中添加补全所用到的 appSettings
<appSettings>
<add key="SeqServer" value="http://localhost:5341/" />
<add key="SeqApiKey" value="" />
</appSettings>
5):在 Startup.cs 中添加如下代码完成注册
using Microsoft.Owin;
using Owin;
using Serilog;
using TestSerilog.App_Start; [assembly: OwinStartupAttribute(typeof(TestSerilog.Startup))]
namespace TestSerilog
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
Log.Logger = SerilogConfig.CreateLogger();
}
}
}
6): 在 HomeController 中的 Index Action 中添加如下代码,测试对应的Debug,Information,Warning 和 Error 方法
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace TestSerilog.Controllers
{
public class HomeController : Controller
{
private ILogger _logger = Log.Logger; public ActionResult Index()
{
_logger.Debug("This is index -- debug.");
_logger.Information("This is index -- information.");
_logger.Warning("This is index -- warning.");
_logger.Error("This is index -- error.");
return View();
} public ActionResult About()
{
ViewBag.Message = "Your application description page."; return View();
} public ActionResult Contact()
{
ViewBag.Message = "Your contact page."; return View();
}
}
}
7):直接 VS 2015 运行之后, 再去 http://localhost:5341/#/events 中观察对应的 log 记录, 如下截图

这样简单的配置 Serilog 就完成了, 同时我们也可以到 C:\ProgramData\Seq\Logs 目录中找到 Log 的文本文件。
更多内容请看如下链接:
http://serilog.net/
https://github.com/serilog/serilog
在Asp.Net MVC 中配置 Serilog的更多相关文章
- asp.net mvc中配置路由默认值(Area中)
public class RouteConfig { ] { "Best.Site.Areas.BestPalace" }; public static void Register ...
- 在ASP.Net MVC 中,如何在Global.asax中配置一个指向Area内部的默认Route
ASP.Net MVC 中配置Route的时候可以设置一个默认的Route. 比如我要在输入http://localhost的时候默认进入http://localhost/home/index.可以在 ...
- ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则
ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)
在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...
- ASP.NET MVC中的错误处理
ASP.NET MVC中的错误的错误处理跨越了两个主要领域:程序异常和路由异常的处理.前者是关于在控制器和视图中捕获错误的;而后者更多是有关重定向和HTTP错误的. 1.在WebConfig中把过滤器 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(1)
介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...
- ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器
ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器一:闲谈一下:1.现在任务跟踪管理系统已经开发快要结束了,抽一点时间来写一下,想一想自己就有成就感啊!! ...
- KindEditor上传本地图片在ASP.NET MVC的配置
http://www.cnblogs.com/upupto/archive/2010/08/24/1807202.html 本文解决KindEditor上传本地图片在ASP.NET MVC中的配置. ...
随机推荐
- Windows快捷键
一.常见用法: F1 显示当前程序或者windows的帮助内容. F2 当你选中一个文件的话,这意味着“重命名” F3 当你在桌面上的时候是打开“查找:所有文件” 对话框 F10或ALT 激活当前程序 ...
- linux下epoll实现机制
linux下epoll实现机制 原作者:陶辉 链接:http://blog.csdn.net/russell_tao/article/details/7160071 先简单回顾下如何使用C库封装的se ...
- Oracle 删除重复数据只留一条
查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 g ...
- 关于编写性能高效的javascript事件的技术
如何能做出高效的web前端程序是我每次做前端开发都会不自觉去考虑的问题.几年前雅虎里牛逼的前端工程师们出了一本关于提升web前端性能的书籍,轰动了整个web开发技术界,让神秘的web前端优化问题成为了 ...
- 多材质(Shader)实现
最近在cocos creator上打算写个U3D中shader功能的插件(能在属性面板调整shader属性). 对其中一个功能有点疑惑,就是U3D中一个渲染物体上可以挂多个材质,后来查询了下,一个物体 ...
- 10、代码块、构造代码块、静态代码块及main方法之间的关系
1.普通代码块: 在方法或语句中出现在{}之间的类容就称为普通代码块,简称代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出现先执行”,即顺序执行. /*下面第一个类时合法的 ...
- frame和bounds
- frame 是一个以**父视图**为坐标系的位置- bounds 是一个以**自身**为坐标系的位置- 如果改变了bounds 那么会影响子控件的显示位置
- [Hadoop] Hadoop学习历程 [持续更新中…]
1. Hadoop FS Shell Hadoop之所以可以实现分布式计算,主要的原因之一是因为其背后的分布式文件系统(HDFS).所以,对于Hadoop的文件操作需要有一套全新的shell指令来完成 ...
- 【Android】Android属性allowBackup安全风险
allowBackup值为true或false,Android API Level 8及其以上Android系统提供了为应用程序数据的备份和恢复功能,当allowBackup标志为true时,用户即可 ...
- 移动端web开发的一些知识点
整理一下自己平时移动端web开发中遇到的问题,也参考一下前辈的一些总结 1.最常见的要数1像素边框了 因为Retine屏的分辨率始终是普通屏幕的2倍,1px的边框在dpr=2的retina屏下会显示成 ...