How to use Serilog in WCF with Autofac?

145 Views Asked by At

I am completely new to wcf and serilog and autofac, and I am trying to integrate all three of them. I am trying to log all events into a txt file and I somehow got it to work using this code in global asax:

 var logFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Log/Log.txt");
        ILogger logger = new LoggerConfiguration()
                  .WriteTo.File(logFilePath)
                  .CreateLogger();
        Log.Logger = logger;
        logger.Information("Application Startup...");

and not this code:

            var builder = new ContainerBuilder();
        builder.RegisterType<Service1>();
        builder.Register<ILogger>((c, p) =>
        {
            return new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.File(logFilePath)
                         .CreateLogger();
        }).SingleInstance();

        AutofacHostFactory.Container = builder.Build();

What is the difference between this two codes? With the first code I can already log information in other classes, but I have come to find out that it is not good practice to manually log. I have tried using dependency injection like below in other classes:

        ILogger _logger;
    public AddCard(ILogger logger)
    {
        _logger = logger;
    }

But for some reason it does not work. I am trying to make serilog log events at a global scale yet I do not understand how serilog and autofac tie in together in wcf. I have more than 40 tabs open trying to piece together what info I can, but Im still finding it hard to understand.

0

There are 0 best solutions below