代码之家  ›  专栏  ›  技术社区  ›  Chris

程序集属性中的Log4net配置不加载配置文件

  •  27
  • Chris  · 技术社区  · 14 年前

    我的bin目录中有以下文件Log4net.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <log4net xmlns="urn:log4net">
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
            <param name="file" value="MyLogFile.log"/>
            <param name="appendToFile" value="false"/>
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%date (%logger) [%5level] - %message" />
            </layout>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%date (%logger) [%5level] - %message" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="FileAppender" />
            <appender-ref ref="ConsoleAppender"/>
        </root>
        <logger name="NHibernate" additivity="false">
            <level value="WARN"/>
        </logger>
    </log4net>
    

    以及my AssemblyInfo.cs文件中的以下代码:

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyTitle("My Project")]
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]
    

    当我运行程序时,我得到以下log4net调试输出:

    log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [D:\Data\Projects\Active\Clients\MyProject\src\MyProject.Importer\bin\Debug\log4net.dll]. (.NET Runtime [4.0.30319.1] on Microsoft Windows NT 6.1.7600.0)
    log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
    log4net: DefaultRepositorySelector: Creating repository for assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null]
    log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] Loaded From [D:\Data\Projects\Active\Clients\MyProject\src\MyProject.Importer\bin\Debug\MyCompany.Framework.dll]
    log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
    log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
    log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
    The thread 'vshost.RunParkingWindow' (0xd30) has exited with code 0 (0x0).
    The thread '<No Name>' (0x15d0) has exited with code 0 (0x0).
    log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]
    

    Log4net加载,但似乎没有处理我的配置文件。当我注释掉AssemblyInfo.cs中的属性并在程序初始化期间运行以下代码时,它将按预期工作:

    var log4netConfig = "Log4net.config";
    var log4netInfo = new FileInfo(log4netConfig);
    log4net.Config.XmlConfigurator.ConfigureAndWatch(log4netInfo);
    

    6 回复  |  直到 11 年前
        1
  •  39
  •   Anthony Mastrean PavanDevarakonda    13 年前

    我也有麻烦用这种方法隆胸log4net。这个 documentation 说你必须在应用程序启动的早期打电话

    如果使用配置属性,则必须调用log4net以允许它读取属性。对LogManager.GetLogger的简单调用将导致读取和处理调用程序集上的属性。 因此,在应用程序启动期间,当然是在加载和调用任何外部程序集之前,必须尽早进行日志记录调用。

    private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
    

    对于kicks,对日志进行任何调用(即使是从append进程中筛选或计算出来的日志,它也应该强制log4net计算您的存储库/继承权)。

    static Program()
    {
        Log.Debug("Application loaded.");
    }
    
        2
  •  7
  •   adnan    8 年前

    最后我找到了简单的解决办法,你可能会得到帮助

    1. 开始功能中的Global.asax

       protected void Application_Start()
       {
         log4net.Config.XmlConfigurator.Configure();
       }
      

    添加命名空间

    using log4net;
    

    在类级别添加此代码行

    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
    1. 在任何操作调用中使用log函数

      log.Error("test error q111..");
      
    2. 配置

      <configuration>
      <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net " />
      </configSection>
      <log4net debug="true">
      
      <!--AdoNet appender is use for write log file into sql server-->
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="Data Source=DESKTOP-NLH31FH; Initial Catalog=SmallBizDb;Integrated Security=true" providerName="System.Data.SqlClient" />
        <commandText value="INSERT INTO [dbo].[Logs] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@logdate,@thread, @loglevel, @logger, @message, @exception)" />
        <parameter>
          <parameterName value="@logdate" />
          <dbType value="DateTime" />
          <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        <parameter>
          <parameterName value="@thread" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@loglevel" />
          <dbType value="String" />
          <size value="50" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@logger" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@message" />
          <dbType value="String" />
          <size value="4000" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@exception" />
          <dbType value="String" />
          <size value="2000" />
          <layout type="log4net.Layout.ExceptionLayout" />
        </parameter>
      </appender>
      <!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->
      
      <root>
        <level value="Debug" />
        <!--<appender-ref ref="RollingLogFileAppender" />-->
        <!--Enable this line if you want write log file into plain text file-->
        <appender-ref ref="AdoNetAppender" />
        <!--Enable this line if you want write log file into sql server-->
      
      </root>
      
      </log4net>
      
      <appSettings>
      <add key="log4net.Internal.Debug" value="true"/>
      </appSettings>
      </configuration>
      

        3
  •  4
  •   rkmorgan    9 年前

    我确实保留 log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config"))); 在应用程序中的Global.asax.cs中。。。所以我不需要把命令带到任何地方。

        4
  •  2
  •   NightShovel    8 年前

    我通过添加 RepositoryAttribute 到有问题的程序集的AssemblyInfo.cs文件。

    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    [assembly: RepositoryAttribute("Your.Namespace.Here")]
    
        5
  •  0
  •   Rahul Jujarey    10 年前

    我正在使用Web.Config节配置记录器并手动记录事件,从global.asax引导记录器

    static ILog logger = LogManager.GetLogger(<LoggerName>);
    
    protected void Application_Start()
    {
       log4net.Config.XmlConfigurator.Configure(); 
    }
    

    尝试从global.asax引导它

        6
  •  0
  •   tech-y    5 年前

    我在windows服务中使用log4Net。我尝试了其他答案中提到的所有可能的选择。

    对我来说,服务的另一个实例还活着,我不得不从任务管理器中删除它。之后,问题就解决了。

        7
  •  -1
  •   jayson.centeno    9 年前
    var log4NetPath = Server.MapPath("~/log4net.config");
    
    FileInfo fileInfo = new FileInfo(log4NetPath);
    
    XmlConfigurator.ConfigureAndWatch(fileInfo);