NOTE: This post is out of date. Please read: Updated Step by step: Serilog with ASP.NET Core for an update.

Last week I wrote about .NET Core and Microsoft Bot Framework and I’m still learning and playing with it. The thing is that once I implemented more features and deployed the bot to Azure it didn’t work. So I had to find a way to log and trace what was happening in order to diagnose and fix the problem.

This time I decided to give a chance to Serilog and as you should know by now, getting the correct dependencies to work with .Net Core is not always easy, so here is my Step by step: Serilog with ASP.NET Core

Add the following dependencies in your project.json file

1    "Serilog": "2.2.0",
2    "Serilog.Extensions.Logging": "1.2.0",
3    "Serilog.Sinks.RollingFile": "2.0.0",
4    "Serilog.Sinks.File": "3.0.0"

Add the following lines to the constructor of your Startup class


1     Log.Logger = new LoggerConfiguration()
2        .MinimumLevel.Debug()
3        .WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "log-{Date}.txt"))
4        .CreateLogger();

Add the following line to the configure method of your Startup class


1     loggerFactory.AddSerilog();

Inject the logger to your services or controllers


 1    public class Chat : IChat
 2    {
 3        // Instancia del logger
 4        ILogger logger;
 5
 6        // Injectamos el logger en el constructor
 7        public Chat(ILogger logger)
 8        {
 9            this.logger = logger;
10        }
11
12        // Método de envío de los mensajes
13        public virtual void SendMessage(string message)
14        {
15            try
16            {
17                // Enví­o de Mensaje
18            }
19            catch (System.Exception ex)
20            {
21                // Enviamos al log los errores.
22                this.logger.LogError(ex.Message);
23            }
24        }
25    }

Learn More

Fundamentals: Logging in .NET Core and ASP.NET Core

Hope it helps!