c# - 为什么 log4net 不写入文件?

标签 c# visual-studio-2012 log4net

我已经添加了 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

我的应用程序配置:

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

我的类(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;
        }
    }
}

最佳答案

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

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

关于c# - 为什么 log4net 不写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13236091/

相关文章:

c# - 使用 nodatime 如何找到两个 ZonedDateTime 对象之间的区别

c# - 使用 MVVM Light 时处理复选框的最佳方法是什么

unit-testing - 中止在 Visual Studio 2012 中运行 MSTest 单元测试

c# - 如何调试大型字符串列表和多维数字数组?

c# - Log4Net:如何将程序内部的日志记录结果输出到一个变量中?

c# - 我想使用 LoadAsync 和 MemoryStream 将数据库中的图像加载到图片框中

c# - 基于严格要求的正则表达式拆分和提取

c# - 使用 linq 过滤列表中的相似项目

json - log4net.Ext.Json 自定义属性未出现在 json 中

java - 如何使用 log4j SMTP 附加程序发送电子邮件