asp.net - web.config asp.net mvc 5 中的 serilog AppSetting

标签 asp.net asp.net-mvc appsettings serilog

我正在研究 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 很陌生,所以我真的很困惑。
请尽快重播。

谢谢你

最佳答案

你需要做的是在appSettings中添加Serilog信息Web.Config 中的标记。

<add key="serilog:using:MSSqlSever" value="Serilog.Sinks.MSSqlServer" />
<add key="serilog:write-to:MSSqlServer.connectionString" value="nameOfConnectionString" />
<add key="serilog:write-to:MSSqlServer.tableName" value="Serilogs" />
<add key="serilog:write-to:MSSqlServer.autoCreateSqlTable" value="true" />

上面的代码可以帮助您登录到 SQL 数据库。
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:write-to:File.path" value="log.txt" />
<add key="serilog:write-to:File.fileSizeLimitBytes" value="" />

以上内容可帮助您写入文本文件。
您在代码中要做的只是调用 log 函数,它会自动记录到您在配置中指定的内容。

您可能需要查看这两个页面以获得更多帮助:
https://github.com/serilog/serilog-sinks-mssqlserver
https://github.com/serilog/serilog-sinks-file

关于asp.net - web.config asp.net mvc 5 中的 serilog AppSetting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40302853/

相关文章:

asp.net-mvc - MVC4 RC WebApi 参数绑定(bind)

asp.net-mvc - 数据注释和 MVC 4

c# - 将 .bot 文件中的设置迁移到 appsettings 时如何修复机器人配置

C# VS.NET 2008 更改每个配置的设置

c# - AppSettings.json 用于 ASP.NET Core 中的集成测试

.net - 编写长轮询 WCF 服务

c# - 我可以覆盖 UserControl 中的 css 类吗?

c# - 如何使用 iTextSharp 设置 PDF 段落或字体行高?

jquery - Jquery Mobile 中的简单表单提交

asp.net - Asp.net MVC 中的自动完成文本框