Search code examples
servicestackserilog

SerilogFactory don't log my messages but it logs exceptions


I want to use the SerilogFactory, the LogFactory is initialized before initializing the AppHost. This is my startup.cs :

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        var logger = new LoggerConfiguration().WriteTo.File("log.txt").CreateLogger();
        LogManager.LogFactory = new SerilogFactory(logger);

        app.Map("/api", api =>
        {
            api.UseServiceStack(new AppHost(env.ApplicationName, Configuration));
        });
   }
}

And this is my sample service :

public class TestService : ServiceStack.Service
{
    public static ILog Log = LogManager.GetLogger(typeof(TestService));

    public bool Get(GetTest request)
    {
        Log.Info("foo");
        Log.Warn("???");

        throw new System.Exception("plop");

        return true;
    }
}

After calling the "GetTest" request, I can see the details of the exception in my "log.txt" file but I can't see the Info "foo" or the Warm "???" in the file.

2019-07-03 18:07:55.597 +02:00 [INF] Initializing Application Tmpi.PocketTournee took 352.2006ms. No errors detected.
2019-07-03 18:08:07.446 +02:00 [ERR] ServiceBase<TRequest>::Service Exception
System.Exception: plop
at Tmpi.PocketTournee.ServiceInterface.TestService.Get(GetTest request) in C:\Projects\VS2017\PocketTournee\WebService\Sources\Tmpi.PocketTournee.ServiceInterface\TestService.cs:line 15

If I initialize the LogFactory AFTER initializing the AppHost, I can see my own logs but this time the exception details and the servicestack init info have disappeared :

2019-07-03 18:25:05.420 +02:00 [INF] foo
2019-07-03 18:25:05.434 +02:00 [WRN] ???

Solution

  • So I've set some breakpoint to watch the type of LogManager.LogFactory :
    Before registering the first LogFactory the type of LogManager.LogFactory is ServiceStack.Logging.NullLogFactory
    *After registering the first LogFactory the type of LogManager.LogFactory is of course ServiceStack.Logging.Serilog.SerilogFactory nothing wrong here
    *But after AppHost initialization the type of LogManager.LogFactory is reverted to ServiceStack.NetCore.NetCoreLogFactory

    Like in this code :

            var logFactory = new SerilogFactory(new LoggerConfiguration()
                .WriteTo.File("log.txt")
                .CreateLogger());
    
    // Here LogManager.LogFactory is {ServiceStack.Logging.NullLogFactory}
    
            LogManager.LogFactory = logFactory;
    
    // Here LogManager.LogFactory is {ServiceStack.Logging.Serilog.SerilogFactory}
    
            app.Map("/api", api =>
            {
                api.UseServiceStack(new AppHost(env.ApplicationName, Configuration));
            });
    
    // Here LogManager.LogFactory is {ServiceStack.NetCore.NetCoreLogFactory}
    
            LogManager.LogFactory = logFactory;