使用serilog

时间:2017-07-24 07:12:14

标签: asp.net-mvc asp.net-core serilog

我使用serilog在我的.netcore MVC项目中添加了日志文件,并且我读取了所有参数appsettings.json文件。 Serilog配置(在appsettings中)是这样的。

在此解决方案中,所有日志都写在一个文件中。但我想写不同的错误信息不同的文件。比如我的X方法错误是写成xMethodLogs.txt和Y方法错误写的是yMethodLogs.txt

MyStartup.cs文件是

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddSerilog();

        app.UseMvc();
        app.UseMvc(routes =>
        {
            routes.MapRoute("default", "api/{controller=Home}/{action=Index}/{id?}/{sad?}");
        });
    }


public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

我的appsettings文件是

"Serilog": { 
    "Using": [ "Serilog.Sinks.RollingFile" ], 
    "MinimumLevel": "Error", 
    "WriteTo": [ { 
        "Name": "RollingFile", 
        "Args": { "pathFormat": "C:\\MyPro\\ServiceLog.txt" } 
    } ], 
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], 
    "Properties": { "Application": "Sample" } 
}

0 个答案:

没有答案