Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is log4net not writing to a file?

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;
        }
    }
}
like image 990
Elad Benda Avatar asked Dec 26 '22 15:12

Elad Benda


2 Answers

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

like image 34
Milen Kindekov Avatar answered Feb 09 '23 08:02

Milen Kindekov


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)]
like image 126
akatakritos Avatar answered Feb 09 '23 07:02

akatakritos