• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Use Log4net In Ninja trader custom project

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Use Log4net In Ninja trader custom project

    Hi Guys,

    I had my strategy packaged into a dll and was able to integrate into ninjatrader successfully. But I am having some troubles configuring my log4net in ninja trader.

    is there a way for me to put my log4net configuration into ninjatrader and have my log output to the target file as I did when I had my dll running in my own test project? (what i did was putting all the configurations into app.config)

    Thanks,

    An

    #2
    Hi An,

    Thanks for the post and welcome to the NinjaTrader forums. Unfortunately we cannot provide support for custom applications, but will leave this thread open for other community members to respond.

    If you would like assistance using NinjaScript to output to a file, please see the following reference samples:
    http://www.ninjatrader.com/support/forum/showthread.php?t=3475
    http://www.ninjatrader.com/support/f...ead.php?t=3477
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I do not know if this will answer all of your questions but this link might get you going in the right direction.
      http://sadi02.wordpress.com/2008/06/...log-in-a-file/

      Comment


        #4
        Thanks ScJohn,

        I did exactly the same in my unit test project which was running perfectly. But I am having troubles using it in Ninjatrader on step 4.

        4. Now put this web.config/app.config file in configuration tag.

        I don't know where I should put it. Do you have similar problem before?

        Thanks,

        An

        Comment


          #5
          Though this is a old thread, I would like to know if anyone was able to resolve the issue. I would like to know where to place the config file ?

          Comment


            #6
            I have Log4Net now working... Yeah not sure why the App.config will not be read so instead redirected to a another config file.

            Steps for my example:
            1) Create directory C:\NinjaTraderLogs
            2) Create config file C:\NinjaTraderLogs\Log4Net.config.
            Example config for Rolling File Appender
            Code:
            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
              <configSections>
                <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
              </configSections>
              <log4net>
                    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
                  <file value="C:\NinjaTraderLogs\NinjaLogs.txt" />
                  <appendToFile value="true" />
                  <rollingStyle value="Size" />
                  <maxSizeRollBackups value="5" />
                  <maximumFileSize value="10MB" />
                  <staticLogFileName value="true" />
                  <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
                  </layout>
                </appender>
                <root>
                  <level value="All" />
                  <appender-ref ref="RollingFileAppender" />
                </root>
              </log4net>
            </configuration>
            3) In some custom Indi or Strategy add following assembly statement :
            Code:
            [assembly: log4net.Config.XmlConfigurator(ConfigFile = "C:\\NinjaTraderLogs\\Log4Net.config", Watch = true)]
            4) In each class (Indi, Strat, etc) you want to use the logger add the following just inside the class:
            Code:
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            5) Now create log statements like
            Code:
            log.Debug("this is my variable = " + variable1);
            There are various log levels that can be used (e.g. log.Debug, log.Error, log.Info, etc). The config file currently is set to log all levels with
            Code:
            <level value="All" />
            This can be changed at runtime as needed.

            You could also create a different config file for each strategy, indi, etc and create separate log files for each if you like.

            Many appenders are available. Some will create xml files, some will post log messages to a database, some will send email, etc...

            Hope this helps.

            Comment


              #7
              Hey tornadoatc thanks for your input.

              I did exactly what you described, and I don't get log4net working for me, probably I'm missing something...

              1 and 2, are done locally and I copied your config file...

              3 I added the following code to C:\Users\<my user>\Documents\NinjaTrader 8\bin\Custom\AssemblyInfo.cs
              Code:
               [assembly: log4net.Config.XmlConfigurator(ConfigFile = "C:\\NinjaTraderLogs\\Log4Net.config", Watch = true)]
              4 In my strategy I wrote:
              Code:
              ...in the strategy class...
              
              private static ILog logr;
              
              protected override void OnStateChange()
              {
                     if(State == State.Configure)
                     {
                            log4net.Config.XmlConfigurator.Configure();
                            logr = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                            logr.Info("logr.Info()");
                     }
              }
              5 I compiled and enabled the strategy which should result, if I understand correctly, in 1 log entry in a newly created log file?

              Thanks in advance for your expertise
              Paul

              Comment


                #8
                I added:
                Code:
                log4net.Config.XmlConfigurator.Configure(new FileInfo("C:\\NinjaTraderLogs\\Log4Net.config"));
                and now it is working...

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by zergrush, Today, 12:17 AM
                5 responses
                17 views
                0 likes
                Last Post NinjaTrader_ChristopherD  
                Started by HiddenPhilosopher, Today, 03:53 AM
                0 responses
                6 views
                0 likes
                Last Post HiddenPhilosopher  
                Started by Leeroy_Jenkins, Today, 03:00 AM
                0 responses
                8 views
                0 likes
                Last Post Leeroy_Jenkins  
                Started by KhaosTrader, Yesterday, 11:04 PM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_Marco  
                Started by rcsingleton, Today, 12:28 AM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_ChrisSch  
                Working...
                X