title: C# ASP.NET Core Serilog add class name and method to log
body: <p>I recently added logging to my ASP.Net Core project. Currently the log writes to a .txt file in this format:</p>
  
  <blockquote>
    <p>{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}</p>
  </blockquote>
  
  <p>For example:</p>
  
  <blockquote>
    <p>2017-11-30 13:58:22.229 +01:00 [Information] Item created in database.</p>
  </blockquote>
  
  <p>This is fine but I would like to have the name of the class that logs and the method that is being executed to this .txt file. For example when Class A writes something to the database using Method B and logs this, I would like to see something like</p>
  
  <blockquote>
    <p>ClassA.MethodB: Item created in database</p>
  </blockquote>
  
  <p>All the classes that log have their Logger injected into their constructors like</p>
  
  <pre><code>public class ClassA
  {
      private readonly ILogger _log;
  
      public ClassA(ILogger&lt;ClassA&gt; log){
          _log = log;
      }
  
      public void AddItemToDb(Item item){
          //Add item
          //On success: 
          _log.LogInfo("Added item to db.");
      }
  }
  </code></pre>
  
  <p>I'm currently using Serilog and using the following LoggerConfiguration:</p>
  
  <pre><code>var logger = new LoggerConfiguration()
      .MinimumLevel.Verbose()
      .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
      .WriteTo.RollingFile(Configuration.GetValue&lt;string&gt;("LogFilePath") + "-{Date}.txt", LogEventLevel.Information)
      .CreateLogger();
  </code></pre>
  
  <p>How can I add the class and method to my logs?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I added a custom outputTemplate to the .WriteTo.Rollingfile() method like so:</p>
  
  <pre><code>"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}) {Message}{NewLine}{Exception}")
  </code></pre>
  
  <p>Which resulted in the namespace plus the class to be added in the log, only the method is missing now</p>