您现在的位置是:网站首页> 编程资料编程资料
Asp.Net MVC中配置Serilog的方法_实用技巧_
2023-05-24
603人已围观
简介 Asp.Net MVC中配置Serilog的方法_实用技巧_
一、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() .Enrich.With() .Enrich.With() .Enrich.With() //.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 > 0) { var titleAttribute = (AssemblyTitleAttribute)attributes[0]; if (titleAttribute.Title.Length > 0) return titleAttribute.Title; } return Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase); } } } } 4):在 Web.config 中添加补全所用到的 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/
- asp.net log4net的使用方法
- C#、ASP.NET通用扩展工具类之LogicSugar
- Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- 使用asp.net MVC4中的Bundle遇到的问题及解决办法分享
- asp.net MVC实现无组件上传图片实例介绍
- ASP.NET MVC DropDownList数据绑定及使用详解
- IIS7 配置大全(ASP.NET 2.0, WCF, ASP.NET MVC,php)
- ASP.NET MVC 5使用X.PagedList.Mvc进行分页教程(PagedList.Mvc)
- ASP.NET MVC中为DropDownListFor设置选中项的方法
相关内容
- 微信公众平台开发教程(八)Session处理问题_实用技巧_
- mvc form表单提交的几种形式整理总结_实用技巧_
- 微信公众平台开发教程(二) 基本原理及消息接口总结_实用技巧_
- .net MVC 连接数据本地数据库三种方法总结_实用技巧_
- .NET异步编程总结----四种实现模式代码总结_实用技巧_
- ASP.NET同步分页MvcPager使用详解_实用技巧_
- 详解ASP.NET-----Repeater数据控件的用法总结_实用技巧_
- Asp.net中安全退出时清空Session或Cookie的实例代码_实用技巧_
- 解决ASP.NET Core Mvc文件上传限制问题实例_实用技巧_
- .NET工厂方法模式讲解_实用技巧_
