1

How can I use Serilog from Controller itself? I have added this in my Configure method in Startup class :

var serilog = new Serilog.LoggerConfiguration().MinimumLevel.Debug().WriteTo.File("C:/Temp/log.txt");
logFact.AddSerilog(serilog);

I also tried something like this:

    public PeopleController(ILoggerFactory logFact)
    {
        _loger = logFact.CreateLogger<PeopleController>();
    }

And after that in action method I wanted to call it like this:

_loger.LogDebug("this is my custom logges");

Issue is that I don't get my log written in my log.txt file. How should I do this properly?

zoranpro
  • 453
  • 4
  • 18

1 Answers1

3

Try some other level than Debug, I've got Serilog working except for the Debug level, not sure if its a bug or a config issue.

Tom
  • 836
  • 7
  • 14
  • I did try it. Same thing. I am also interested if I am doing this properly. Should I actually pass `ILoggerFactory` into the controller? I was able to make it to write in the file with creating Logging Middleware that I added in `Configure` method and when I throw the exception in action. – zoranpro May 14 '15 at 18:42
  • 2
    That's correct, there's a bug in the mapping of the `Debug` level; we (the Serilog) team are working on some improvements to ASP.NET 5 support currently: https://github.com/aspnet/Logging/pull/182. – Nicholas Blumhardt May 14 '15 at 22:05
  • I did worked with `Error` level. Not sure why didn't worked first time I changed level. Can you tell me @NicholasBlumhardt if I use Serilog properly in the `Controller` by passing `ILoggerFactory` in the constructor? – zoranpro May 15 '15 at 06:52
  • 1
    Using Serilog's own `Log` class, or a Serilog `ILogger` is preferable; http://nblumhardt.com/2015/05/diagnostic-logging-in-dnx-asp-net-5/ might help clarify things. Ah and the linked package fixes the level mapping bug :-) – Nicholas Blumhardt May 17 '15 at 04:01
  • @Tom - thank you very much, this issue has been driving me nuts for a couple of days - I thought there was a problem with the way I was configuring it :) – Nick May 19 '16 at 14:43