无法正确配置 log4net
cannot configure log4net correctly
我曾尝试通过代码配置 log4net,但当我将其用作 Logger.Info("new bar");
:
时出现以下错误
at NinjaTrader.Indicator.Logger.Info(Object msg) in c:\Users\Documents\NinjaTrader 7\bin\Custom\Indicator\STSVer1.cs:line 610
at NinjaTrader.Indicator.STSVer1.OnBarUpdate() in c:\Users\Documents\NinjaTrader 7\bin\Custom\Indicator\STSVer1.cs:line 108
Object reference not set to an instance of an object.
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
ILog 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);
}
}
在Logger
-class静态构造函数中,你没有初始化属性Log
,而是初始化了一个名为Log
的新局部变量。因此,当您从 Info
方法内部调用它时,属性 Log
是 null
。
从代码的以下行中删除类型名称 ILog
。
ILog Log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
顺便说一句,您注释掉的正下方行做了正确的事情 - 关于正确初始化 属性。
我曾尝试通过代码配置 log4net,但当我将其用作 Logger.Info("new bar");
:
at NinjaTrader.Indicator.Logger.Info(Object msg) in c:\Users\Documents\NinjaTrader 7\bin\Custom\Indicator\STSVer1.cs:line 610
at NinjaTrader.Indicator.STSVer1.OnBarUpdate() in c:\Users\Documents\NinjaTrader 7\bin\Custom\Indicator\STSVer1.cs:line 108
Object reference not set to an instance of an object.
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
ILog 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);
}
}
在Logger
-class静态构造函数中,你没有初始化属性Log
,而是初始化了一个名为Log
的新局部变量。因此,当您从 Info
方法内部调用它时,属性 Log
是 null
。
从代码的以下行中删除类型名称 ILog
。
ILog Log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
顺便说一句,您注释掉的正下方行做了正确的事情 - 关于正确初始化 属性。