为什么log4net没有写入文件?

时间:2012-11-05 16:20:30

标签: c# visual-studio-2012 log4

我添加了log4net的所有部分,但它没有写入文件 我正在使用VS2012 LoadTest项目。

运行LoadTest项目时System.Console.WriteLineDebug.WriteLine()都不起作用。

我添加了汇编信息行:

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

我已经:
- 添加了webconfig部分
- 初始化已配置的XML初始化程序
- 用适当的日志初始化新的log4net

我的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>

我的课程:

//------------------------------------------------------------------------------
// <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;
        }
    }
}

由于

3 个答案:

答案 0 :(得分:5)

如果您希望log4net从app.config中读取,则必须将其添加到AssemblyInfo.cs文件中:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

答案 1 :(得分:4)

查看 app.config

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

这可以确保应用程序在启动时出现错误时拒绝在.NET 4.0上启动。

如果您使用的是Framework .NET4.5,则log4net不支持它。请参阅http://logging.apache.org/log4net/release/features.html

中的框架部分

答案 2 :(得分:1)

使用<param name = "Activity" value="Settings_CacheExplosion.txt" />代替<file value="Settings_CacheExplosion.txt" />。在你的xml配置部分。