具有动态字段名的Log4Net静态类

时间:2016-06-04 19:19:01

标签: c# log4net

为了实现Ninjatrader 7,我有一个静态Log4Net Logger,我在其中配置设置。但是这个类被其他类多次实例化。我无法区分记录信息的实例。 是否可以为每个实例设置配置然后记录,以便区分实例。 例如 Logger.setInstrument(Instrument.fullname)//可能是$ EURUSD或$ EURJPY或$ GBPUSD 然后当我使用Logger.info或Logger.Error记录时,我会得到带有前缀的符号吗?

这是一些示例数据

2016-06-04 15:04:53,163 [NT main thread] INFO  [NinjaTrader.Indicator.Logger]   5/30/2016 4:04:16 AM123 123 wait4longpullback 123 

2016-06-04 15:04:53,164 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:17 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,164 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:18 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,165 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:19 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,165 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:19 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,166 [NT主要主题] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:20 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,166 [NT主要主题] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:20 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,167 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:22 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,167 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:22 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,168 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:23 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,169 [NT主线程] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:23 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,169 [NT主线程] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:24 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,170 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:25 AM123 123 wait4longpullback 123

2016-06-04 15:04:53,170 [NT主线] INFO [NinjaTrader.Indicator.Logger] 5/30/2016 4:04:25 AM123 123 wait4longpullback 123

  public static class Logger
{
    private static log4net.ILog Log { get; set; }

    static Logger()
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.RemoveAllAppenders(); /*Remove any other appenders*/

        FileAppender fileAppender = new FileAppender();
        fileAppender.AppendToFile = true;
        fileAppender.LockingModel = new FileAppender.MinimalLock();
        fileAppender.File = @"c:\temp\logevents.txt";
        PatternLayout pl = new PatternLayout();
        pl.ConversionPattern = "%d [%2%t] %-5p [%-10c]   %m%n%n";
        pl.ActivateOptions();
        fileAppender.Layout = pl;
        fileAppender.ActivateOptions();

        log4net.Config.BasicConfigurator.Configure(fileAppender);
        //Test logger
        // Log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        Log = log4net.LogManager.GetLogger(typeof(Logger));
    }

    public static void Error(object msg)
    {
        Log.Error(msg);
    }

    public static void Error(object msg, Exception ex)
    {
        Log.Error(msg, ex);
    }

    public static void Error(Exception ex)
    {
        Log.Error(ex.Message, ex);
    }

    public static void Info(object msg)
    {
        Log.Info(msg);
    }
}

0 个答案:

没有答案