Serilog not working from configuration in asp.net core 2.2 API

狂风中的少年 提交于 2019-12-22 04:37:25

问题


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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
           .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
           .UseStartup<Startup>();
}


public class Startup
{
    public IContainer Container { get; private set; }
    public Startup(IConfiguration configuration)
    {
        Log.Warning("test");
        Configuration = configuration;
    }

}

appsettings.json

{
    "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Default": "Information",
        "Microsoft": "Information",
        "System": "Information"
      }
    },
    "WriteTo": [

      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "C:\\test.txt",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",
          "restrictedToMinimumLevel": "Information"
        }
      }
    ]
  },

  "AllowedHosts": "*"
}

I have all the packages installed

  • Serilog,
  • Serilog.AspCore,
  • Serilog.Settings.Configuration,
  • Serilog.Sink.File

回答1:


Your config is for RollingFile but your package list says Serilog.Sinks.File. These are different. You need to add the Serilog.Sinks.RollingFile package and it should start working.

If you want to use the File sink (which, as @Kirk mentioned in the comments, is the recommended option now) then you need to change the settings to

"WriteTo": [
    {
        "Name": "File",
        "Args": {
            "path": "C:\\test.txt",
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",
            "restrictedToMinimumLevel": "Information"
        }
    }

NOTE

The pathFormat should be path for the File sink

See the Serilog File Sink Documentation




回答2:


It seems the problem was writing directly to c: and not using File in config which seems not to work, but writing to a different directory works.

Here is the final solution for anyone coming across this

Program.cs

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
              WebHost.CreateDefaultBuilder(args)
                  .UseStartup<Startup>()
                  .UseSerilog();
}

Startup.cs

public Startup(IConfiguration configuration)
{
        var logConfiguration = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration);

        Log.Logger = logConfiguration.CreateLogger();
        Configuration = configuration;

}

appsettings.json

"Serilog": {
"WriteTo": [
  {
    "Name": "File",
    "Args": {
      "path": "C:\\temp\\log.txt",
      "rollingInterval": "Day"
    }
  }
]

}




回答3:


This is not an answer but an observation. I had observed a definite problem here. I have got three settingss files.

  1. appsettings.json,
  2. appsettings.Development.json,
  3. appsettings.VivekDev.json

I have selected the profile from launchSettings.json such that the environment is VivekDev as follows.

"ASPNETCORE_ENVIRONMENT": "VivekDev"

Now when i have the following setting in appsettings.VivekDev.json without having any Serilog setting in appsettings.json, then things are working fine.

  "Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "log.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  },

But when I have

"Serilog": {
  "WriteTo": [ "Debug" ]
},

in appsettings.json, this is somehow overriding the setting in appsettings.VivekDev.json. As per my understanding this is incorrect and the setting inside of appsettings.VivekDev.json should override appsettings.json.

And this is happening only in case of File sink. If I use other sink such as console then things are working as expected. That is the serilog setting inside of appsettings.VivekDev.json is correctly overriding the serilog setting of appsettings.json.

Something is definitely buggy here.

So of the now, I have removed the serilog settings in the appsettings.json and kept the serilog write to file setting in appsettings.VivekDev.json.



来源:https://stackoverflow.com/questions/53877037/serilog-not-working-from-configuration-in-asp-net-core-2-2-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!