简体   繁体   English

Serilog文件接收器未记录到文件

[英]Serilog File sink not logging to file

i am trying to get Serilog file sink working on my ASP.Net core 2.2 application based on the the documentation . 我试图根据文档在我的ASP.Net core 2.2应用程序上运行Serilog文件接收器。 I am not able to see logs in my application. 我在应用程序中看不到日志。 What am I missing? 我想念什么?

Program.cs: Program.cs:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;

namespace Scrubber
{
  public class Program
  {
    private static string _environmentName;

    public static void Main(string[] args)
    {
      try
      {
        var iWebHost = CreateWebHostBuilder(args).Build();
        var configuration = new ConfigurationBuilder()
          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
          .AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true)
        .Build();

        var logger = new LoggerConfiguration()
          .ReadFrom.Configuration(configuration.GetSection("Serilog"))
          .CreateLogger();
        Log.Logger = logger;
        Log.Information("Application starting");
        iWebHost.Run();
      }
      catch(Exception exception)
      {
        Log.Error(exception.ToString());
      }
      finally
      {
        Log.CloseAndFlush();
      }
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
          WebHost.CreateDefaultBuilder(args)
          .ConfigureLogging((hostingContext, config) =>
          {
            //config.ClearProviders();
            _environmentName = hostingContext.HostingEnvironment.EnvironmentName;
          })
          .UseStartup<Startup>();
    }
}

Appsettings.development.json: Appsettings.development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "log.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  } 
}

A possible reason is that the App didn't load the configuration at all. 一个可能的原因是该应用程序根本没有加载配置。

Note you set up the configuration in the following way: 请注意,您可以通过以下方式设置配置:

  var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true) .Build(); 
  1. You didn't set a base path for ConfigurationBuilder 您没有为ConfigurationBuilder设置基本路径
  2. You registered an optional json file by setting optional: false 您通过设置optional: false注册一个optional json文件optional: false

As a result, if the json file doesn't reside in the right place, it fails silently. 结果,如果json文件不在正确的位置,则它会静默失败。

I suggest you could change your code as below: 我建议您可以如下更改代码:

// get the real path 
//     or by reflection 
//     or by injection, 
var path = Directory.GetCurrentDirectory();       // assume the current directory

var configuration = new ConfigurationBuilder()
    .SetBasePath(path)                                                       // set the right path
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)  //  make it required
    .AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true)
    .Build();

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM