4

I have added all parts of log4net, however it doesn't write to the file.
I'm working with the VS2012 LoadTest project.

Neither the System.Console.WriteLine or Debug.WriteLine() work, when running the LoadTest project.

I've added the assembly info line:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]   //For log4net 1.2.10.0

I've:
- added webconfig section
- initialized an configured an XML initializer
- initialized new log4net with the proper log

My app.config:

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Settings_CacheExplosion.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

</configuration>

My class:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WebAndLoadTestProject1
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using log4net;
    using log4net.Core;
    using Microsoft.VisualStudio.TestTools.WebTesting;


    public class Settings_CacheExplosion : WebTest
    {
        private static readonly ILog activityLog = LogManager.GetLogger("Activity"); 

        private static int _ctidsCounter { get; set; }

        public static int CtidsCounter
        {

            get
            {

                if (_ctidsCounter == 2000)
                {
                    _ctidsCounter = 1000;
                }
                return _ctidsCounter++;
            }
            set
            {
                _ctidsCounter = value;
            }
        }

        public Settings_CacheExplosion()
        {
            this.PreAuthenticate = true;

            CtidsCounter = 1000;

            log4net.Config.XmlConfigurator.Configure();
        }


        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            WebTestRequest request1 = new WebTestRequest("http://clientservice.mam.qasite-services.com/settings");

            request1.Method = "POST";

            Debug.WriteLine(string.Format("ctid={0}", CtidsCounter));

            request1.QueryStringParameters.Add("ctid", CtidsCounter.ToString(), false, false);
            StringHttpBody request1Body = new StringHttpBody();
            request1Body.ContentType = "";
            request1Body.InsertByteOrderMark = false;
            request1Body.BodyString = "";
            request1.Body = request1Body;

            activityLog.Debug(string.Format("ctid={0}", CtidsCounter));
            //Console.WriteLine(string.Format("ctid={0}", CtidsCounter));

            yield return request1;
            request1 = null;
        }
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

3 Answers3

5

If you want log4net to read from the app.config, you have to add this to your AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
akatakritos
  • 9,836
  • 1
  • 23
  • 29
4

Looking at your app.config:

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

This works to ensure that the app will refuse to start on .NET 4.0 with an error on startup.

If you are using Framework .NET4.5, log4net does not support it. See frameworks section in http://logging.apache.org/log4net/release/features.html

Milen Kindekov
  • 1,903
  • 1
  • 23
  • 31
  • Create a new project using the NET4 framework. No other way if you want to use log4net that is. – Milen Kindekov Nov 05 '12 at 16:51
  • staying at 4.5 what logging option do i have? – Elad Benda Nov 05 '12 at 16:52
  • Did you change the setting in your app.config to be: ``, did you recreate your project? – Milen Kindekov Nov 05 '12 at 16:56
  • changed in the project properties and it changed the app.config automatically to: ` `. I have rebuilt and the file isn't created – Elad Benda Nov 05 '12 at 16:58
  • In your app.config, try changing your `` to ` ` If this does not work, try creating a brand new NET4 test project, following the same steps you undertook. I cannot refer you to other logging libraries, you can always go for custom logging, but that is almost always a hassle and you are losing quite a lot of functionality. – Milen Kindekov Nov 05 '12 at 17:08
  • And last but not least, reading through the log4net [FAQ](http://logging.apache.org/log4net/release/faq.html), log4net cannot log to a FileAppender from a Web Application. – Milen Kindekov Nov 05 '12 at 17:17
1

use <param name = "Activity" value="Settings_CacheExplosion.txt" /> instead of <file value="Settings_CacheExplosion.txt" />. in your xml configuration section.

Riccardo
  • 1,490
  • 2
  • 12
  • 22