I am trying to figure out how to get Serilog to log the class name and method/line number, like I would see with log4cxx in C++.
I tried my best to grab all the relevant bits out of the real code I am working on and come up with a minimal example.
I've also been Googling Serilog left and right, but I am not finding good documentation. I suppose it is because there are so many libraries on top of the base serilog and each needs there own docs to tell me how to do things.
I can see the basics on configuration at https://github.com/serilog/serilog/wiki/Configuration-Basics , but this seems to use a TextWriter sink from a seperate Serilog library and a custom formatter, both of which I don't really understand.
I can also find examples on stack overflow that use the simple configuatation and the enrich call to log the class and method names.
C# ASP.NET Core Serilog add class name and method to log
I am not able to get it to log them. How can I get this to log the class and method name or line number while still using the custom formatter and TextWriter?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Serilog;
using Serilog.Events;
using Serilog.Formatting;
namespace SerilogDemo {
// Someone else made this, I just changed the name to protect the innocent
public class SomeonesLogTextFormatter : ITextFormatter
{
public void Format(LogEvent logEvent, TextWriter output)
{
output.Write(logEvent.Level);
output.Write(": ");
logEvent.MessageTemplate.Render(logEvent.Properties, output);
output.WriteLine();
if (logEvent.Exception != null)
{
output.WriteLine(logEvent.Exception);
}
}
}
public class SomeClass
{
private Serilog.ILogger _log = Serilog.Log.ForContext<SomeClass>();
public SomeClass()
{
_log.Debug("SomeClass has been instantiated");
}
public void Foo()
{
_log.Debug("Foo has been called");
}
}
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.TextWriter(textWriter: Console.Out, formatter: new SomeonesLogTextFormatter())
.CreateLogger();
var poop = new SomeClass();
poop.Foo();
}
}
}