Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    Nov 21, 2019 @ 01:01
    Nathan Woulfe
    0

    Specify logging output file when injecting ILogger

    Scenario is so:

    • site currently uses a mix of LogHelper and ILog (log4net interface) to access logging methods
    • with an eye on v8, I'm replacing LogHelper and ILog with ILogger, injected via AutoFac
    • currently we have an attribute used to decorate the ILog ctor param, which describes the output log file to use
    • while ILogger and ILog are both interfaces into log4net, they don't appear to be immediately interchangeable

    Current ctor implementation looks like the below. Using ILog in place of the update to ILogger works, ILogger results in a null param value:

    public MyConstructor([LogName("MyCustomLogName")] ILogger logger) 
    {
        _logger = logger;
        // etc..
    }
    

    The Autofac logging module is based on this one https://autofaccn.readthedocs.io/en/latest/examples/log4net.html but rather than logging based on the component type, it uses LogManager to get the log by the name provided as the attribute param. ILog exposes

    The existing Umbraco Logging service doesn't expose any method for getting/setting the log file, and doesn't appear to have any internals to do so either.

    On poking around a bit, if I grab the ILogger property, CanWrite is false (true on ILog property types), so I can't call SetValue on the property to set the logger. At least, that's my read on the situation...

    Has anyone done similar or have an alternative solution?

Please Sign in or register to post replies

Write your reply to:

Draft