一、.net mvc

serilog在mvc中配置比较简单,配置好后在项目入口处初始化一下就可以使用。

引入的包:多数情况下,sinks选择输出到文件或者数据库。具体的sinks可查看官网:https://github.com/serilog/serilog/wiki/Provided-Sinks

  1. Serilog
  2. //控制台输出
  3. Serilog.Sinks.Console
  4. //发送邮件
  5. Serilog.Sinks.Email
  6. //将日志写入到文件
  7. Serilog.Sinks.File
  8. //推送日志至数据库
  9. Serilog.Sinks.MssqlServer
  1. 首先创建一个类,用来自定义配置serilog的消息模板、输出位置等
  1. public class LogLevel
  2. {
  3. //增根据actionName分类存储日志
  4. public static void InitLoggerDefaultWithName(string actionName)
  5. {
  6. string LogFilePath(string LogEvent) => System.Web.Hosting.HostingEnvironment.MapPath($@"~/LogLevel/{LogEvent}/{actionName}.log");
  7. string SerilogOutputTemplate =
  8. "{NewLine}{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}" + $@"{actionName}" +
  9. new string('-', 50);
  10. Log.Logger = new LoggerConfiguration()
  11. .Enrich.FromLogContext()
  12. .MinimumLevel.Debug() // 所有Sink的最小记录级别
  13. .WriteTo.Logger(lg =>
  14. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.File(LogFilePath("Debug"),
  15. rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
  16. .WriteTo.Logger(lg =>
  17. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(
  18. LogFilePath("Information"), rollingInterval: RollingInterval.Day,
  19. outputTemplate: SerilogOutputTemplate))
  20. .WriteTo.Logger(lg =>
  21. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(
  22. LogFilePath("Warning"), rollingInterval: RollingInterval.Day,
  23. outputTemplate: SerilogOutputTemplate))
  24. .WriteTo.Logger(lg =>
  25. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"),
  26. rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
  27. .WriteTo.Logger(lg =>
  28. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"),
  29. rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
  30. .CreateLogger();
  31. }
  32. public static void InitLoggerDefault()
  33. {
  34. string LogFilePath(string LogEvent) => System.Web.Hosting.HostingEnvironment.MapPath($@"~/LogLevel/{LogEvent}/log.log");
  35. string SerilogOutputTemplate =
  36. "{NewLine}{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}" +
  37. new string('-', 50);
  38. Log.Logger = new LoggerConfiguration()
  39. .Enrich.FromLogContext()
  40. .MinimumLevel.Debug() // 所有Sink的最小记录级别
  41. .WriteTo.Logger(lg =>
  42. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.File(LogFilePath("Debug"),
  43. rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
  44. .WriteTo.Logger(lg =>
  45. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(
  46. LogFilePath("Information"), rollingInterval: RollingInterval.Day,
  47. outputTemplate: SerilogOutputTemplate))
  48. .WriteTo.Logger(lg =>
  49. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(
  50. LogFilePath("Warning"), rollingInterval: RollingInterval.Day,
  51. outputTemplate: SerilogOutputTemplate))
  52. .WriteTo.Logger(lg =>
  53. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"),
  54. rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
  55. .WriteTo.Logger(lg =>
  56. lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"),
  57. rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
  58. .CreateLogger();
  59. }
  60. }
  1. Global.aspx.cs中初始化
  1. //初始化Serilog
  2. LogLevel.InitLoggerDefaultWithName(MethodBase.GetCurrentMethod().Name);
  1. 使用的时候直接调用Log.Information(msg, args)

二、.net core

.net core配置相对复杂一点,如果只有一个启动项,基本上配置与mvc相同。

引入的包:多数情况下,sinks选择输出到文件或者数据库。具体的sinks可查看官网:https://github.com/serilog/serilog/wiki/Provided-Sinks

  1. Serilog.AspNetCore
  2. //控制台输出
  3. Serilog.Sinks.Console
  4. //发送邮件
  5. Serilog.Sinks.Email
  6. //将日志写入到文件
  7. Serilog.Sinks.File
  8. //推送日志至数据库
  9. Serilog.Sinks.MssqlServer

1.输出到控制台

  1. public static void Main(string[] args)
  2. {
  3. Log.Logger = new LoggerConfiguration()
  4. .MinimumLevel.Information()//最小的记录等级
  5. .MinimumLevel.Override("Microsoft", LogEventLevel.Information)//对其他日志进行重写,除此之外,目前框架只有微软自带的日志组件
  6. .WriteTo.Console()//输出到控制台
  7. .CreateLogger();
  8. Log.Information("info");
  9. Log.Error("err");
  10. CreateHostBuilder(args).Build().Run();
  11. }

Serilog配置 - 图1

在终端中不同等级的日志颜色不同 Serilog提供了两个类(SystemConsoleThemes和AnsiConsoleThemes)用于主题的变化

<font style="color:rgb(0, 0, 0);background-color:rgb(242, 244, 245);"> .WriteTo.Console(theme: SystemConsoleTheme.Colored)</font>

Serilog配置 - 图2

<font style="color:rgb(0, 0, 0);background-color:rgb(242, 244, 245);">.WriteTo.Console(theme: AnsiConsoleTheme.Code)</font>

Serilog配置 - 图3

也可以自定义输出的模板
  1. .WriteTo.Console(theme: AnsiConsoleTheme.Code,
  2. outputTemplate: "发生时间:{Timestamp: HH:mm:ss.fff} 事件级别:{Level} 详细信息:{Message}{NewLine}{Exception}")

Serilog配置 - 图4

我们也可以将这些配置项写入到appsettings.json文件中 导包:Serilog.Settings.Configuration (在Serilog.AspNetCore3.1.0版本及以上已经集成了,提示如下:) Serilog配置 - 图5 在appsettings.json加入以下json块儿
  1. "Serilog": {
  2. "WriteTo": [
  3. {
  4. "Name": "Console",
  5. "Args": {
  6. "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
  7. "outputTemplate": "发生时间:{Timestamp: HH:mm:ss.fff} 事件等级:{Level} 详细信息:{Message}{NewLine}{Exception}"
  8. }
  9. }
  10. ]
  11. }

program.cs配置:

  1. Log.Logger = new LoggerConfiguration()
  2. .MinimumLevel.Information()
  3. .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
  4. .ReadFrom.Configuration(new ConfigurationBuilder()
  5. .AddJsonFile("appsettings.json")
  6. .Build())
  7. .CreateLogger();c

2.将日志写入到文件

  1. Log.Logger = new LoggerConfiguration()
  2. .MinimumLevel.Information()
  3. .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
  4. .ReadFrom.Configuration(new ConfigurationBuilder()
  5. .AddJsonFile("appsettings.json")
  6. .Build())
  7. .WriteTo.File(Path.Combine("MyLogs", "log"), rollingInterval: RollingInterval.Day)//文件生成到当前路径 rollingInterval:RollingInterval.Day:按天生成文件
  8. .CreateLogger();

Serilog配置 - 图6

将配置信息添加到json中
  1. "Serilog": {
  2. "WriteTo": [
  3. {
  4. "Name": "Console",
  5. "Args": {
  6. "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
  7. "outputTemplate": "发生时间:{Timestamp: HH:mm:ss.fff} 事件等级:{Level} 详细信息:{Message}{NewLine}{Exception}",
  8. }
  9. },
  10. {
  11. "Name": "File",
  12. "Args": {
  13. "path": "MyLogs/log.txt",
  14. "rollingInterval": "Day",
  15. "outputTemplate": "发生时间:{Timestamp: HH:mm:ss.fff} 事件等级:{Level} 详细信息:{Message}{NewLine}{Exception}"
  16. }
  17. }
  18. ]
  19. }

将这句代码注释掉:.WriteTo.File(Path.Combine("MyLogs", "log"), rollingInterval: RollingInterval.Day)。配置中有path。

3.将日志推送到数据库

  1. Log.Logger = new LoggerConfiguration()
  2. .MinimumLevel.Information()
  3. .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
  4. .ReadFrom.Configuration(new ConfigurationBuilder()
  5. .AddJsonFile("appsettings.json")
  6. .Build())
  7. .WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)//从左至右四个参数分别是数据库连接字符串、表名、如果表不存在是否创建、最低等级。Serilog会默认创建一些列。
  8. .CreateLogger();

Serilog配置 - 图7

4.发送邮件

  1. .WriteTo.Email(new EmailConnectionInfo()
  2. {
  3. EmailSubject = "系统警告,请速速查看!",//邮件标题
  4. FromEmail = "291***@qq.com",//发件人邮箱
  5. MailServer = "smtp.qq.com",//smtp服务器地址
  6. NetworkCredentials = new NetworkCredential("291***@qq.com", "###########"),//两个参数分别是发件人邮箱与客户端授权码
  7. Port = 587,//端口号
  8. ToEmail = "183***@163.com"//收件人
  9. })
如果对客户端授权码不熟悉的同学可移步此博客:https://www.cnblogs.com/zhangnever/p/11926020.html 发送成功! Serilog配置 - 图8 完整代码:
  1. public class Program
  2. {
  3. public static void Main(string[] args)
  4. {
  5. Log.Logger = new LoggerConfiguration()
  6. .MinimumLevel.Information()
  7. .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
  8. .ReadFrom.Configuration(new ConfigurationBuilder()
  9. .AddJsonFile("appsettings.json")
  10. .Build())
  11. .WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)//从左至右四个参数分别是数据库连接字符串、表名、如果表不存在是否创建、最低等级。Serilog会默认创建一些列。
  12. .WriteTo.Email(new EmailConnectionInfo()
  13. {
  14. EmailSubject = "系统警告,请速速查看!",//邮件标题
  15. FromEmail = "291***@qq.com",//发件人邮箱
  16. MailServer = "smtp.qq.com",//smtp服务器地址
  17. NetworkCredentials = new NetworkCredential("291***@qq.com", "###########"),//两个参数分别是发件人邮箱与客户端授权码
  18. Port = 587,//端口号
  19. ToEmail = "188***@163.com"//收件人
  20. })
  21. .CreateLogger();
  22. Log.Information("info");
  23. Log.Error("err");
  24. CreateHostBuilder(args).Build().Run();
  25. }
  26. public static IHostBuilder CreateHostBuilder(string[] args) =>
  27. Host.CreateDefaultBuilder(args)
  28. .ConfigureWebHostDefaults(webBuilder =>
  29. {
  30. webBuilder.UseSerilog()//在宿主机启动的时候配置serilog,与微软ILogger进行整合
  31. .UseStartup<Startup>();
  32. });
  33. }
我们在用的时候可以直接在控制器中注入ILogger 例如微软的天气预报的控制器:
  1. private readonly ILogger<WeatherForecastController> _logger;
  2. public WeatherForecastController(ILogger<WeatherForecastController> logger)
  3. {
  4. _logger = logger;
  5. } //直接在接口中使用: _logger.LogInformation("success");
最好配合过滤器一起使用,达到AOP的效果 请看此篇博客:https://www.yuque.com/schafferyy/net/yuqwge #### 5.个性化配置serilog 上述设计的都是最基本的配置信息,能用,但不是很好用。但是对于serilog的熟悉还是有帮助的,毕竟一步一个脚印嘛! 1. 基本配置类似于mvc,不同点在于增加了对于记录级别的重写: .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) csharp public class LogConfig { //增根据actionName分类存储日志 public static void InitLoggerDefaultWithName(string actionName) { string LogFilePath(string LogEvent) => $@"D:/Logs/exam/{LogEvent}/{actionName}.log"; string SerilogOutputTemplate = "{NewLine}{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}" + new string('-', 50); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Information() // 所有Sink的最小记录级别 .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File( LogFilePath("Information"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File( LogFilePath("Warning"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File( LogFilePath("Error"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File( LogFilePath("Fatal"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .CreateLogger(); } public static void InitLoggerDefault() { string LogFilePath(string LogEvent) => $@"D:/Logs/exam/{LogEvent}/log.log"; string SerilogOutputTemplate = "{NewLine}{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}" + new string('-', 50); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Information() // 所有Sink的最小记录级别 .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File( LogFilePath("Information"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File( LogFilePath("Warning"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File( LogFilePath("Error"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg =>lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File( LogFilePath("Fatal"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .CreateLogger(); } } 2. 在program.cs中初始化:
  1. LogConfig.InitLoggerDefaultWithName(MethodBase.GetCurrentMethod().DeclaringType.Name);
  1. 还有一步就是与微软ILogger进行整合,在host后面,而不是在webBuilder后面:
  1. return Host.CreateDefaultBuilder(args)
  2. .UseSerilog()//在宿主机启动的时候配置serilog,与微软ILogger进行整合
  3. .ConfigureWebHostDefaults(webBuilder =>
  4. {
  5. webBuilder
  6. .Inject()
  7. .UseStartup<Startup>();
  8. });
  1. 使用Log.Information(msg, args)
  2. 设置请求管道:
在 Startup.cs 的 中的Configure 请求管道中添加 UseSerilogRequestLogging

如果日志中需要添加请求相关信息,可以在Startup.cs(用于配置中间件管道)中配置。RequestLoggingMiddleware被包含在Serilog.AspNetCore中,可以被用于为每个请求添加一个单一的“摘要”日志消息。如果您已经完成了上一节中的步骤,则添加这个中间件将变得很简单。在您的Startup类中,在您想要记录日志的位置使用UseSerilogRequestLogging()进行调用:

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  2. {
  3. if (env.IsDevelopment())
  4. {
  5. app.UseDeveloperExceptionPage();
  6. }
  7. else
  8. {
  9. app.UseExceptionHandler("/Error");
  10. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  11. app.UseHsts();
  12. }
  13. app.UseHttpsRedirection();
  14. app.UseStaticFiles();
  15. app.UseSerilogRequestLogging(); // <-- Add this line
  16. app.UseRouting();
  17. app.UseAuthorization();
  18. app.UseEndpoints(endpoints =>
  19. {
  20. endpoints.MapRazorPages();
  21. });
  22. }
重要的是UseSerilogRequestLogging()调用应出现在诸如MVC之类的处理程序之前。 中间件不会对管道中出现在它之前的组件进行时间或日志记录。通过将UseSerilogRequestLogging()放在它们之后,可以将其用于从日志中排除杂乱的处理程序,例如UseStaticFiles()。 为了减少每个HTTP请求需要构造,传输和存储的日志事件的数量。 在同一事件上具有许多属性还可以使请求详细信息和其他数据的关联更加容易。 默认情况下,以下请求信息将作为属性添加:
  • 请求方法
  • 请求路径
  • 状态码
  • 响应时间
您可以使用UseSerilogRequestLogging()上的选项回调来修改用于请求完成事件的消息模板,添加其他属性或更改事件级别:
  1. app.UseSerilogRequestLogging(options =>
  2. {
  3. // 自定义消息模板
  4. options.MessageTemplate = "Handled {RequestPath}";
  5. // 发出调试级别的事件,而不是默认事件
  6. options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug;
  7. //将其他属性附加到请求完成事件
  8. options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
  9. {
  10. diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
  11. diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
  12. };
  13. });
与ASP.NET Core的中间件管道一样,顺序很重要。当请求到达RequestLoggingMiddleware中间件时,它将启动计时器,并将请求传递给后续中间件进行处理。当后面的中间件最终生成响应(或抛出异常),则响应通过中间件管道传递到请求记录器,并在其中记录了结果并写入概要日志信息。 Serilog只能记录到达中间件的请求。在上面的例子中,我已经在StaticFilesMiddleware之后添加了RequestLoggingMiddleware 。因此如果请求被UseStaticFiles处理并使管道短路的话,日志将不会被记录。鉴于静态文件中间件非常嘈杂,而且通常这是人们期望的行为(静态文件进行短路,不需要进行记录),但是如果您也希望记录对静态文件的请求,则可以在管道中serilog中间件移动到更早的位置。 总结: 使用Serilog.AspNetCore的请求日志记录中间件来减少为每个ASP.NET Core请求生成的日志数,同时仍记录摘要数据。如果您已经在使用Serilog,则非常容易启用。只需在您的Startup.cs文件中调UseSerilogRequestLogging() 当请求到达此中间件时,它将启动计时器。当后续的中间件生成响应(或引发异常)时,响应将通过中间件管道返回到请求记录器,记录器记录结果并编写摘要日志消息。 添加请求日志记录中间件之后,您可以过滤掉默认情况下在ASP.NET Core 3.0中生成的更多基础结构日志,而不会丢失有用的信息。

具体请看:如何利用Serilog的RequestLogging来精简ASP.NET Core的日志输出

了解更多可以查看:

https://github.com/serilog/serilog-aspnetcore https://github.com/mthamil/AspNetCore.Serilog.RequestLoggingMiddleware