Serilog:RollingFile在带有'appsettings.json'的asp.net核心中不起作用

时间:2019-06-28 06:04:27

标签: c# asp.net-core logging serilog

我正在使用asp.net core(网站)和Serilog进行日志记录。

已安装Nuget软件包

Serilog
Serilog.AspNetCore
Serilog.Extensions.Logging
Serilog.Extensions.Logging.File
Serilog.Settings.Configuration
Serilog.Sinks.File
Serilog.Sinks.RollingFile
Microsoft.Extensions.Logging

Main.cs文件中的设置为:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
             .ConfigureAppConfiguration((WebHostBuilderContext, configurationBuilder) =>
             {
                 configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                 configurationBuilder.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true);
                 configurationBuilder.AddEnvironmentVariables();
             })
             .ConfigureLogging((hostingContext, loggingBuilder) =>
             {
                 loggingBuilder.ClearProviders();
                 loggingBuilder.AddSerilog();
             })
            .UseStartup<Startup>();  
}

正在做什么:

在Startup.cs文件中,如果我编写以下代码,则会创建日志文件。在这里,我在代码本身中指定配置。

  public void Configure(IApplicationBuilder app,
                                IHostingEnvironment env,
                                ILoggerFactory loggerFactory)
    {
     string logFilePath = Path.Combine(env.ContentRootPath, "Logs/ImportManager2-{Date}.txt");

    Log.Logger = new LoggerConfiguration()
       .MinimumLevel.Debug()
       .WriteTo.RollingFile(logFilePath)
       .CreateLogger();

     loggerFactory.AddSerilog();

     --
     --
    }

当我使用具有以下内容的appsettings.json文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MyDatabaseLocal": "Server=_;Database=_;MultipleActiveResultSets=true;Connection Timeout=333;Integrated Security=SSPI",
    "OtherDatabaseLocal": "Server=_;Database=_;MultipleActiveResultSets=true;Connection Timeout=333;Integrated Security=SSPI"
  },
  "Serilog": {
    "MinimumLevel": "Debug",
    "Override": {
      "Microsoft": "Warning"
    },
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File"
    ],
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "path": "Logs/MyProject.txt"
        }
      }
    ]
  }
}

并更改了

之类的startup.cs文件
public void Configure(IApplicationBuilder app,
                            IHostingEnvironment env,
                            ILoggerFactory loggerFactory)
{

 Log.Logger = new LoggerConfiguration()
             .ReadFrom.Configuration(Configuration.GetSection("Serilog"))
             .CreateLogger();

 loggerFactory.AddSerilog();
 --
 --
}

此后未创建文件。可能是什么问题呢。

1 个答案:

答案 0 :(得分:1)

以下电话与您的期望不太吻合:

.ReadFrom.Configuration(Configuration.GetSection("Serilog"))

使用此扩展方法时,Serilog希望在您通过的配置中会有一个名为Serilog 的部分。在您的示例中,您正在传递Serilog部分本身,但随后在其中寻找另一个Serilog部分

将呼叫更新为仅通过Configuration,就像这样:

.ReadFrom.Configuration(Configuration)

Serilog将找到您的Serilog部分并使用其中的设置。