i am in process of using Log4Net and want to refactor the logging. I added the Log4Net config to the indicator but realized that every time i called to log a message, its going to initialize log4net. is there a better way? using System; using log4net; using System.IO; namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator { private static ILog logr = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private void ConfigureLog4Net() { log4net.Config.XmlConfigurator.Configure(new FileInfo(Core.Globals.UserDataDir + @"bin\custom\Log4Net.config")); log4net.Config.XmlConfigurator.Configure(); } public void LogMessage(Object o){ ConfigureLog4Net(); logr.Info(o); } public void LogException(Exception e){ ConfigureLog4Net(); logr.Fatal(e); } } }
Just do the setup once. Code: private static ILog logr = null; private void ConfigureLog4Net() { if(logr) return; logr = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); log4net.Config.XmlConfigurator.Configure(new FileInfo(Core.Globals.UserDataDir + @"bin\custom\Log4Net.config")); log4net.Config.XmlConfigurator.Configure(); } or use the constructor: Code: public Indicator() { ConfigureLog4Net(); }
Probably I don't know, I'm not a C# programmer. https://stackoverflow.com/questions/1580509/partial-class-constructors
you are right, i cannot do the same with a strategy. its a pickle NinjaScript File Error Code Line Column StrategyExtensions.cs Type 'NinjaTrader.NinjaScript.Strategies.Strategy' already defines a member called 'Strategy' with the same parameter types CS0111 10 10 using System; using log4net; using System.IO; namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy { private static ILog logr = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public Strategy() { ConfigureLog4Net(); } private void ConfigureLog4Net() { log4net.Config.XmlConfigurator.Configure(new FileInfo(Core.Globals.UserDataDir + @"bin\custom\Log4Net.config")); log4net.Config.XmlConfigurator.Configure(); } public void LogMessage(Object o){ logr.Info(o); } public void LogException(Exception e){ logr.Info(e.ToString()) ; logr.Fatal(e); } } }
Do you absolutely need to use a partial class? Is that how NinjaTrader works? Seems like a massive oversight not to be able to use a constructor.