1

I'm using Serilog ASP.NET Core for my Blazor (Server) project, I configure the logger via appsettings.json and this is how my output template looks like:

"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] ] [{SourceContext}] {Message:lj}{NewLine}{Exception}"

I use builder.Host.UseSerilog() to add it to the app and ILogger to inject it into my components.

What I'd like to have is to display class name only instead of showing full assembly+class name like it does with {SourceContext}.

Is it possible?

I already tried this one: serilog format SourceContext for showing only assembly name

After that the configuration of the logger (appsettings.json) didn't work.

That other question has a code based approach, whereas I prefer a configuration only one via appsettings.json.

pfx
  • 20,323
  • 43
  • 37
  • 57
Sentouki
  • 85
  • 1
  • 9
  • 1
    @DavidG I understand your reasoning about this question being a dupe, but the difference here is in asking for a solution via `appsettings.json` settings. Maybe a title update would fix that. That other question has a code approach answer. – pfx May 05 '22 at 12:31
  • @pfx The other question doesn't specify a code-based or config-based approach, which is why I thought your answer would work there too. – DavidG May 05 '22 at 12:33

1 Answers1

3

You can apply an expression via an ExpressionTemplate.

Serilogs GitHub page shows a recipe for only logging the type name and no namespace.

Trim down SourceContext to a type name only:

Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)

This expression takes advantage of LastIndexOf() returning -1 when no . character appears in SourceContext, to yield a startIndex of 0 in that case.


Your appsettings.json configuration has to specify to use an ExpressionTemplate. For the Console logger that looks like below.

"Serilog": {
  "MinimumLevel": {
    "Default": "Debug",
  },
  "WriteTo": [
    {
      "Name": "Console",
      "Args": {
        "formatter": {
          "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
          "template": "{@t:HH:mm:ss} - {@l:u} - {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}: {@m}\n{@x}"
        }
      }
    }
  ]
}

Notice that you'll need to include NuGet packages Serilog.Expressions and Serilog.Settings.Configuration starting from version 3.3.0.

<PackageReference Include="Serilog.Expressions" Version="3.3.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
pfx
  • 20,323
  • 43
  • 37
  • 57
  • @pfx thank you, that's exactly what I wanted – Sentouki May 05 '22 at 11:09
  • @DavidG that's not exactly a duplicate....but ok, btw thanks for closing my previous question without giving me chance to clarify – Sentouki May 05 '22 at 11:17
  • 1
    @DavidG This answer would not suit there, since the request for the assembly name can't be retrieved from a settings only approach, since `SourceContext` is a namespace+class `string`. Nor is there an alternative to get the assembly from. I tend to believe that it is this what makes this question and answer different. – pfx May 05 '22 at 16:14