title: .NET Core Serilog Add username to logs via enrichment or middleware
body: <p>I am trying to add username to my logs. The documentation shows this:</p>
  
  <pre><code>class ThreadIdEnricher : ILogEventEnricher
  {
      public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
      {
          logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
                  "ThreadId", Thread.CurrentThread.ManagedThreadId));
      }
  }
  </code></pre>
  
  <p>However, I've read somewhere else that I need to use middleware to add the usernames to the logs.</p>
  
  <p>This is what I have so far:</p>
  
  <p>Middleware to get the current users username</p>
  
  <pre><code>public class UserNameEnricher 
  {
      private readonly RequestDelegate next;
  
      public UserNameEnricher(RequestDelegate next)
      {
          this.next = next;
      }
  
      public Task Invoke(HttpContext context)
      {
          LogContext.PushProperty("UserName", context.User.Identity.Name);
  
          return next(context);
      }
  }
  </code></pre>
  
  <p>Startup.cs</p>
  
  <pre><code>app.UseAuthentication();
  app.UseMiddleware&lt;UserNameEnricher&gt;();
  </code></pre>
  
  <p>Program.cs</p>
  
  <pre><code>Log.Logger = new LoggerConfiguration()
     .ReadFrom.Configuration(Configuration)
     .Filter.ByExcluding(Matching.FromSource("Microsoft"))
     .Filter.ByExcluding(Matching.FromSource("System"))
     .Enrich.FromLogContext()
     .CreateLogger();
  </code></pre>
  
  <p>However despite all these configurations, the usernames still aren't appearing. What am I missing?</p>
  
  <p>Serilog Configuration:</p>
  
  <pre><code>"Serilog": {
      "MinimumLevel": "Information",
      "Override": {
        "Microsoft": "Critical",
      },
      "WriteTo": [
        {
          "Name": "MSSqlServer",
          "Args": {
            "connectionString": "Server=.\\SQLEXPRESS;Database=App-7661DEE4-C53F-4E49-B140-2AFAA2C85927;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true;",
            "schemaName": "App",
            "tableName": "EventLogs"
          }
        }
      ]
    },
  </code></pre>