c# - How to use Serilog with Unity? -


recencely discoverd serilog ( structured logging .net ) , saw lot of advantages. face problems . have 4 projects, 1 web, 1 infrastructure , other 2 windows services, want declare serilog configuration once , use multiple times. , want use dependency injection. have been searching web 3 days , did not find thing usefull, please 1 me.

for example want class 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 dont know should put line of code :

log.logger = new loggerconfiguration()             .writeto.console()             .createlogger(); 

tnx in advance.

first off, in unity configuration, serilog resolving properly, need use injectionfactory, 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 implementation i'm not abstracting serilog, code above missing link in case. imylogger needs parameter inject ilogger, , work out.

that solves mvc part: can inject imylogger controllers in mvc.

which brings locate in solution. because of needs regarding services, need have separate inversion of control project (mysolution.inversionofcontrol) contains bindings. then, instance in web site's unitywebactivator.cs, can this:

    /// <summary>integrates unity when application starts.</summary>     public static void start()      {         //this important part:         var container = unityconfig.getconfiguredcontainer(); //this static class in inversionofcontrol project.          //this generic:         filterproviders.providers.remove(filterproviders.providers.oftype<filterattributefilterprovider>().first());         filterproviders.providers.add(new unityfilterattributefilterprovider(container));          dependencyresolver.setresolver(new unitydependencyresolver(container));     } 

do same services, , should go!


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -