开发者

How to configure a default target in NLog

I am trying to convert existing logging library used by a bunch of apps to use NLog. The existing logging code has no indirection, logging calls go straight from the caller to a webservice call. The existing logging code is implemented as a static singleton. I need to do this in such a way that existing applications that use this library do not need configuration or code changes when they pick up the changed logging library. Later I can go update applications on an as needed basis, configuring a new logging target or changing the code to log to NLog directly.

To do this, I was going to make the static logging code go through NLog. Existing logging calls will be routed through NLog, and the existing webservice call will be wrapped in a custom NLog target.

To make this work on legacy apps without changing them, I need to programmatically set the custom target as the default (when none are configured in a config file). I'd like to do this without making config changes on the numerous existing applications, so I need to do this programmatically.

The problem is... its not working. Any thoughts on this approach? Below is the code I tried to add to hook in the existing logger class to create the default target.

I wouldn't mind going to log4net either. Just looking at the APIs I chose NLog initially as the nam开发者_如何学运维es made more sense to me.

public static class LegacyLogger
{
    static LegacyLogger()
    {
        if (LogManager.Configuration == null
            || LogManager.Configuration.GetConfiguredNamedTargets().Count == 0)
        {
            if (LogManager.Configuration == null)
            {
                LogManager.Configuration = new LoggingConfiguration();
            }

            //LogManager.Configuration.AddTarget("LegacyLogger", new NLog.Targets.DebuggerTarget());
            LogManager.Configuration.AddTarget("LegacyLogger", new LegacyLoggerTarget());
        }

        _logger = LogManager.GetCurrentClassLogger();
    }


Ok, if you're new to NLog, as I was, be advised the examples installed with the installer cover about everything. In this case, I needed:

public static class LegacyLogger
{
    static LegacyLogger()
    {
        if (LogManager.Configuration == null
            || LogManager.Configuration.GetConfiguredNamedTargets().Count == 0)
        {
            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(new LegacyLoggerTarget(), LogLevel.Trace);
        }

        _logger = LogManager.GetCurrentClassLogger();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜