# SerilogDemo **Repository Path**: gavinhome/serilog-demo ## Basic Information - **Project Name**: SerilogDemo - **Description**: asp.net core项目中,在appsettings.json配置Serilog,将不同级别的存到不同文件 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-03-07 - **Last Updated**: 2023-03-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: AspNetCore, Serilog, loglevel, logoutput, logfilter ## README # SerilogDemo #### 介绍 在appsettings.json配置Serilog,将不同级别的存到不同文件。 #### 使用说明 1. 安装Serilog相关的包: ```xml net6.0 enable enable ``` 2. 在appsettings.json中添加Serilog配置: ```json "Serilog": { "MinimumLevel": { "Default": "Debug", "Override": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "WriteTo": [ { "Name": "Logger", "Args": { "configureLogger": { "Filter": [ { "Name": "ByIncludingOnly", "Args": { "expression": "(@Level = 'Debug')" } } ], "WriteTo": [ { "Name": "File", "Args": { "path": "Logs/Debug/log.log", "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}", "rollingInterval": "Day", "retainedFileCountLimit": 7 } } ] } } }, { "Name": "Logger", "Args": { "configureLogger": { "Filter": [ { "Name": "ByIncludingOnly", "Args": { "expression": "(@Level = 'Information')" } } ], "WriteTo": [ { "Name": "File", "Args": { "path": "Logs/Info/log.log", "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}", "rollingInterval": "Day", "retainedFileCountLimit": 7 } } ] } } }, { "Name": "Logger", "Args": { "configureLogger": { "Filter": [ { "Name": "ByIncludingOnly", "Args": { "expression": "(@Level = 'Warning')" } } ], "WriteTo": [ { "Name": "File", "Args": { "path": "Logs/Warning/log.log", "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}", "rollingInterval": "Day", "retainedFileCountLimit": 7 } } ] } } }, { "Name": "Logger", "Args": { "configureLogger": { "Filter": [ { "Name": "ByIncludingOnly", "Args": { "expression": "(@Level = 'Error')" } } ], "WriteTo": [ { "Name": "File", "Args": { "path": "Logs/Error/log.log", "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}", "rollingInterval": "Day", "retainedFileCountLimit": 7 } } ] } } }, { "Name": "Logger", "Args": { "configureLogger": { "Filter": [ { "Name": "ByIncludingOnly", "Args": { "expression": "(@Level = 'Fatal')" } } ], "WriteTo": [ { "Name": "File", "Args": { "path": "Logs/Fatal/log.log", "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}", "rollingInterval": "Day", "retainedFileCountLimit": 7 } } ] } } } ], "Enrich": [ "FromLogContext", "WithMachineName" ], "Properties": { "Application": "SerilogDemo" } }, ``` 3. 在asp.net core的启动程序里启用serilog: ```C# using Serilog; using Serilog.Formatting.Json; builder.Services.AddLogging(logBuilder => { Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .Enrich.FromLogContext() .WriteTo.Console(new JsonFormatter()) .CreateLogger(); logBuilder.AddSerilog(); }); ``` 4. 在需要日志输出的地方添加输出不同级别日志代码,例如: ```C# public IActionResult Index() { _logger.LogDebug("Writing to log file with DEBUG severity level."); _logger.LogInformation("Writing to log file with INFORMATION severity level."); _logger.LogWarning("Writing to log file with WARNING severity level."); _logger.LogError("riting to log file with ERROR severity level."); _logger.LogCritical("Writing to log file with CRITICAL severity level."); return View(); } ``` #### 测试输出 ![image](log_test.png) #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request