简体   繁体   English

为什么 log4net 不写入文件?

[英]Why is log4net not writing to a file?

I have added all parts of log4net, however it doesn't write to the file.我已经添加了 log4net 的所有部分,但是它没有写入文件。
I'm working with the VS2012 LoadTest project.我正在使用 VS2012 LoadTest 项目。

Neither the System.Console.WriteLine or Debug.WriteLine() work, when running the LoadTest project.在运行 LoadTest 项目时, System.Console.WriteLineDebug.WriteLine()都不起作用。

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 - 添加了 webconfig 部分
- initialized an configured an XML initializer - 初始化并配置了一个 XML 初始化器
- initialized new log4net with the proper log - 使用正确的日志初始化新的 log4net

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

If you want log4net to read from the app.config, you have to add this to your AssemblyInfo.cs file:如果你想让 log4net 从 app.config 中读取,你必须将它添加到你的AssemblyInfo.cs文件中:

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

Looking at your app.config :查看您的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.这可以确保应用程序拒绝在 .NET 4.0 上启动并出现启动错误。

If you are using Framework .NET4.5, log4net does not support it.如果您使用的是 Framework .NET4.5,则 log4net 不支持它。 See frameworks section in http://logging.apache.org/log4net/release/features.html请参阅http://logging.apache.org/log4net/release/features.html中的框架部分

use <param name = "Activity" value="Settings_CacheExplosion.txt" /> instead of <file value="Settings_CacheExplosion.txt" /> .使用<param name = "Activity" value="Settings_CacheExplosion.txt" />而不是<file value="Settings_CacheExplosion.txt" /> in your xml configuration section.在您的 xml 配置部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM