8

Recencely I discoverd serilog ( structured logging for .net ) and saw a lot of its advantages. But I face some problems with it. I have 4 projects, one is web, one is infrastructure and the other two are windows services, I want to declare serilog configuration once and use it multiple times. And also I want to use it with dependency injection. I have been searching web for three days now, but I did not find any thing useful, please some one help me.

For example I want this class to be my logging class.

public interface IMyLogger
{
    void Information(string message, object[] parameters);
}

public class MyLogger : IMyLogger
{
    public MyLogger()
    {
    }
    public void Information(string message, object[] parameters)
    {
        Log.Information("LogType : {LogType} - Operation : {Operation}", parameters);
    }
}
public class UserClass
{
private readonly IMyLogger _myLogger;
public UserClass(IMyLogger myLogger)
        {
            _myLogger = myLogger;
        }
}

Now I don't know where I should put this line of code:

Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .CreateLogger();

Tnx in advance.

1
  • For future user here is detail tutorial available Commented Jun 25, 2020 at 14:31

1 Answer 1

8
+50

First off, in your Unity configuration, to get Serilog resolving properly, you need to use an InjectionFactory, like this:

        container.RegisterType<ILogger>(new ContainerControlledLifetimeManager(), new InjectionFactory((ctr, type, name) =>
        {
            ILogger log = new LoggerConfiguration()
                .WriteTo.Console() //Your serilog config here
                .CreateLogger();

            return log;
        }));   

In my implementation I'm not abstracting Serilog, but the code above is the missing link in any case. IMyLogger just needs to parameter inject ILogger, and everything will work itself out.

That solves the MVC part: you can inject IMyLogger into your controllers in MVC.

Which brings us to where to locate this in your solution. Because of your needs regarding services, you probably need to have a separate Inversion of Control project (MySolution.InversionOfControl) that contains your bindings. Then, for instance in your web site's UnityWebActivator.cs, you can do something like this:

    /// <summary>Integrates Unity when the application starts.</summary>
    public static void Start() 
    {
        //This is the important part:
        var container = UnityConfig.GetConfiguredContainer(); //This is a static class in the InversionOfControl project.

        //This is generic:
        FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
        FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

Do the same for your services, and you should be good to go!

8
  • This works nicely. I noticed though that the default lifetime manager if none specified in Unity 4.0.1 is the transient lifetime manager. Which means that every call to resolve the ILogger is going to execute the injection factory and returna new instance. I believe the logger should use ContainerControllerLifetimeManager instead? Commented May 11, 2016 at 0:44
  • @dotnetguy Better late than never, I am updating this now. :) Commented Mar 16, 2017 at 13:16
  • 2
    For everyone else: ContainerControllerLifetimeManager does the best job of mimicking the singleton behavior Serilog was intended for. Other lifetime managers work, but this might be slightly more efficient than creating a new Serilog instance for each request. Commented Mar 16, 2017 at 13:42
  • 1
    ContainerControlledLifetimeManager and HierarchicalLifetimeManager are the only ones that call the dispose method. So, using anything else may affect your flushing of the log. Like @BrianMacKay, I also think container controlled is better for SeriLog.
    – MIWMIB
    Commented May 22, 2018 at 2:11
  • How do you go about configuring the logger (such as minimum level) through the factory? There's a weird catch 22 here. I use a command line parameter to determine if debug logging is enabled or not. However, processing command line arguments happens after the composition root is set up. But I need that input argument to configure the LoggerConfiguration to use a minimum level of debug or info. Commented Mar 14, 2021 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.