我正在研究Serilog asp.net MVC 5.一切正常但我想将设置移动到web.config而不是代码 . 我需要移动数据库连接,文件路径 . 而且我必须为数据库指定两个不同的级别,为文件指定一个级别 .
这是代码
var logFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"xxx.Web-{Environment.MachineName}.log");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Trace(LogEventLevel.Debug, "{Timestamp:u} [{Level}] {SourceContext}:: {CorrelationId} {Message}{NewLine}{Exception}")
.WriteTo.LiterateConsole(LogEventLevel.Debug, "{Timestamp:u} [{Level}] {SourceContext}:: {CorrelationId} {Message}{NewLine}{Exception}")
.WriteTo.RollingFile(logFileName, LogEventLevel.Debug,
"{Timestamp:u} [{Level}] {MachineName} {SourceContext}:: {CorrelationId} {Message}{NewLine}{Exception}",
retainedFileCountLimit: 31, fileSizeLimitBytes: null)
.WriteTo.MSSqlServer(connectionString, "LogUsers", LogEventLevel.Information)
.Enrich.WithExceptionDetails()
.Enrich.With<HttpRequestIdEnricher>()
.Enrich.FromLogContext()
.CreateLogger();
附: connectionString变量来自web.config .
我对serilog很新,所以我真的很困惑 . 如果你能尽快重播请 .
谢谢
1 回答
您需要做的是在Web.Config中的
appSettings
标记中添加Serilog信息 .上面的代码可以帮助您登录到SQL数据库 .
以上内容可帮助您写入文本文件 .
您在代码中所要做的就是调用日志函数,它会自动记录您在配置中指定的内容 .
您可能需要查看这两个页面以获得更多帮助:
https://github.com/serilog/serilog-sinks-mssqlserver
https://github.com/serilog/serilog-sinks-file