0

I have a question about the use of Serilog and the NetCore6 appsettings.json. I want to get in my log file "SourceContext". I was reading some questions here on stackoverflow, trying many examples, but no solution was satisfactory. The following test code does not return any results. You probably have a concept problem of how this works. I would like you to please guide me. Thank you!

NuGet

    <PackageReference Include="Serilog" Version="2.11.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
    <PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
    <PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
    <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
    <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
    <PackageReference Include="Serilog.Expressions" Version="3.4.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
    <PackageReference Include="Serilog.Sinks.Map" Version="1.0.2" />

Program.cs

IConfiguration configuration = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                    .Build();

Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

appsettings.json

"Serilog": {
    "Using": [
      "Serilog.Sinks.File",
      "Serilog.Sinks.Console"
    ],

    "MinimumLevel": {
      "Default": "Verbose"
    },

    "Enrich": [
      "WithOpenTracingContext",
      "FromLogContext",
      "WithMachineName",
      "WithEnvironmentUserName",
      "WithProcessId",
      "WithThreadId"
    ],

    "WriteTo": [

        {
          "Name": "Console",
          "Args": {
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [SourceContext: {SourceContext}] {Message}{NewLine}{Exception}"
          }
        }
    ],

    "Properties": {
          "Application": "app name"
    }
}
  • this blog might be helpful, https://www.code4it.dev/blog/serilog-logging-scope – jmvcollaborator Aug 12 '22 at 01:50
  • ok no, in the blog it doesn't mention anything about the appsettings to set and get sourcecontext. For example here https://stackoverflow.com/questions/48470090/serilog-format-sourcecontext-for-showing-only-assembly-name, here in this question it uses in its appsettings it uses sourcecontext and in the output of the logger the controller class appears where the log was made, that's what I want. – Leandro Quinteros Aug 12 '22 at 12:59

0 Answers0