Giter Site home page Giter Site logo

jdylanmc / unitylog4netextension Goto Github PK

View Code? Open in Web Editor NEW

This project forked from roblevine/unitylog4netextension

0.0 1.0 0.0 425 KB

A simple extension to Unity that enables log4net ILog dependencies to be injected into consuming classes, with the ILog instance being configured as the correct logger for the consuming class.

Home Page: http://blog.roblevine.co.uk/net/using-log4net-with-unity/

License: Other

C# 99.91% Batchfile 0.09%

unitylog4netextension's Introduction

UnityLoggingExtensions

This library was born out of a desire to have a simple extension to Unity that would enable log4net ILog dependencies to be injected into consuming classes, with the ILog instance being the configured as the correct logger for the consuming class.

A fairly standard pattern for declaring a log4net logger in a class is:

public class MyClass
{
    private static readonly ILog logger = LogManager.GetLogger(typeof(MyClass));
}

I want to acheive a similarly configured logger, but have it passed in at construction time:

public class MyClass
{
    private readonly ILog logger;
    
    public MyClass(ILog logger)
    {
        this.logger = logger;
    }
}

There has been some discussion online as to how best to acheive this:

http://stackoverflow.com/questions/6846342/how-to-inject-log4net-ilog-implementations-using-unity-2-0 http://davidkeaveny.blogspot.co.uk/2011/03/unity-and-log4net.html http://blog.baltrinic.com/software-development/dotnet/log4net-integration-with-unity-ioc-container

The solutions discussed and presented in these articles (in particular the last one), inspired me to write this extension as an improvement over Kenneth Baltrinic's documented solution. Although his suggested solution works - it involves some stack walking to identify the Type of the logger to create. I felt that this should be available from within Unity itself without the need to resort to handling StackTrace/StackFrame classes.

In order to acheive the desired end result, three extensions have been written. Two are directly related to this, and one to help with testing:

CreationStackTrackerExtension This extension is responsible for mainting a stack of the type creation heirarchy. At the creation of any given type via unity, the stack provided by this extension will show the parent class, grandparent, etc.

Log4NetExtension Relying on the extension above, this intercepts any request for an ILog instance and creates an ILog instance by calling LogManager.GetLogger(Type t) using the type of the parent class (ie the class the logger is being injected into)

This can be registered as follows:

var container = new UnityContainer();
container.AddNewExtension<Log4NetExtension>();

Once registered, any ILog dependencies will resolve to a log4net logger with a type name matching the type it will be injected into.

CreationStackReporterExtension Relying on the CreationStackTrackerExtension extension above, this extension will resolve any dependency of type UnityObjectCreationStack, and inject an instance that has a copy of the object creation stack, as maintained by CreationStackTrackerExtension.

var container = new UnityContainer();
container.AddNewExtension<CreationStackReporterExtension>();

Once registered, any UnityObjectCreationStack dependencies will resolve to an instance that contains the type creation stack

public class MyClass
{
    public MyClass(UnityObjectCreationStack unityObjectCreationStack)
    {

    }
}

See http://blog.roblevine.co.uk/net/using-log4net-with-unity/ for more info.

NOTE: This is now available as a NuGet package: https://www.nuget.org/packages/UnityLog4NetExtension/

NOTE: Tests are written using Machine Specifications [https://github.com/machine/machine.specifications]. If you are using ReSharper as your test runner, you'll need to run the following batch file to get MSpec to work with the ReSharper runner:

UnityLoggingExtensions\packages\Machine.Specifications.0.5.10\tools\InstallResharperRunner.x.x.bat

unitylog4netextension's People

Contributors

roblevine avatar

Watchers

Dylan McCurry avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.