I have tried to configure log4net via code but I am getting the following error when I use it as 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);
}
}
In the Logger
-class static constructor, you don't initialize the property Log
, but a new local variable called Log
. Thus the property Log
is null
, when you call it from inside your Info
method.
Remove the type name ILog
from the following line of your code.
ILog Log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
BTW, the line just below, which you commented out, did the right thing - with respect to correctly initializing the property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.