Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Serilog with Unity?

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.

like image 265
jsDevia Avatar asked May 30 '15 07:05

jsDevia


People also ask

What is Serilog used for?

Serilog is a . NET library that provides diagnostic logging to files, the console, and almost everywhere you would like. Serilog can be used in classic . NET Framework applications and for applications running on the latest and greatest .

What is Serilog C#?

Serilog is a logging library for . NET and C# that allows for more detailed and structured logging than the default . NET logging library. Serilog can be used to log information about application events, errors, and performance metrics.

Can you stop logging with Serilog?

For example, if you are using Serilog you should configure Log. Logger and LibLog will use it. Also you can disable logging by setting LogProvider. IsDisabled to true . "


1 Answers

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!

like image 60
Brian MacKay Avatar answered Oct 17 '22 17:10

Brian MacKay