4

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

运行 LoadTest 项目时, System.Console.WriteLineor都不起作用。Debug.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;
        }
    }
}
4

3 回答 3

5

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

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
于 2012-11-05T16:26:36.147 回答
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中的框架部分

于 2012-11-05T16:38:39.557 回答
1

使用<param name = "Activity" value="Settings_CacheExplosion.txt" />而不是<file value="Settings_CacheExplosion.txt" />. 在您的 xml 配置部分中。

于 2012-11-05T17:16:42.663 回答