• Log4net多程序集使用独立的配置文件


     

    多程序集在相同的进程中使用独立的配置文件。

    在平时的软件开发中常常会使用一些第三方的库,第三方的库与我们的程序都使用log4net来做日志记录,在默认的情况下我们使用相同的配置来使用log4net的的配置文件。但是在很多情况下,我们需要的日志配置与第三方类库的日志配置信息需要是独立的。在这种情况下如何解决此问题?

    解决方案结构如下:

    应用程序SimpleApp与SharedModule使用相同的配置,第三方类库SimpleModule使用独立的配置。

    clip_image002

    SimpleApp EntryPoint的源代码

    #region Copyright & License

    //

    // Copyright 2001-2005 The Apache Software Foundation

    //

    // Licensed under the Apache License, Version 2.0 (the "License");

    // you may not use this file except in compliance with the License.

    // You may obtain a copy of the License at

    //

    // http://www.apache.org/licenses/LICENSE-2.0

    //

    // Unless required by applicable law or agreed to in writing, software

    // distributed under the License is distributed on an "AS IS" BASIS,

    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

    // See the License for the specific language governing permissions and

    // limitations under the License.

    //

    #endregion

    // Configure logging for this assembly using the 'SimpleApp.exe.log4net' file

    //配置log4net监视的配置文件后缀为log4net的文件

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

    // The following alias attribute can be used to capture the logging

    // repository for the 'SimpleModule' assembly. Without specifying this

    // attribute the logging configuration for the 'SimpleModule' assembly

    // will be read from the 'SimpleModule.dll.log4net' file. When this

    // attribute is specified the configuration will be shared with this

    // assemby's configuration.

    //[assembly: log4net.Config.AliasRepository("SimpleModule")]

    namespace SimpleApp

    {

    using System;

    /// <summary>

    /// Summary description for Class1.

    /// </summary>

    public class EntryPoint

    {

    // Create a logger for use in this class

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    public static void Main(string[] args)

    {

    args=new string[] {"3","7"};

    if (log.IsInfoEnabled) log.Info(args);

    if (args.Length != 2)

    {

    log.Error("Must supply 2 command line arguments");

    }

    else

    {

    int left = int.Parse(args[0]);

    int right = int.Parse(args[1]);

    int result = 0;

    if (log.IsDebugEnabled) log.Debug("Adding [" + left + "] to [" + right + "]");

    result = (new SimpleModule.Math()).Add(left, right);

    if (log.IsDebugEnabled) log.Debug("Result [" + result + "]");

    Console.Out.WriteLine(result);

    if (log.IsDebugEnabled) log.Debug("Subtracting [" + right + "] from [" + left + "]");

    result = (new SharedModule.Math()).Subtract(left, right);

    if (log.IsDebugEnabled) log.Debug("Result [" + result + "]");

    Console.Out.WriteLine(result);

    }

    Console.Read ();

    }

    }

    }

    SimpleApp.exe.log4net 配置

    <?xml version="1.0" encoding="utf-8" ?>

    <!-- This section contains the log4net configuration settings -->

    <log4net>

    <!-- Define some output appenders -->

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >

    <layout type="log4net.Layout.PatternLayout">

    <conversionPattern value="[SimpleAppConfig] %date [%thread] %-5level %logger - %message%newline" />

    </layout>

    </appender>

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">

    <file value="log-file.txt" />

    <!-- Example using environment variables in params -->

    <!-- <file value="${TMP}\log-file.txt" /> -->

    <appendToFile value="true" />

    <!-- An alternate output encoding can be specified -->

    <!-- <encoding value="unicodeFFFE" /> -->

    <layout type="log4net.Layout.PatternLayout">

    <header value="[Header] " />

    <footer value="[Footer] " />

    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />

    </layout>

    <!-- Alternate layout using XML

    <layout type="log4net.Layout.XMLLayout" /> -->

    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->

    <root>

    <level value="DEBUG" />

    <appender-ref ref="ConsoleAppender" />

    <appender-ref ref="LogFileAppender" />

    </root>

    </log4net>

    SimpleModule 项目

    Math.cs 代码

    #region Copyright & License

    //

    // Copyright 2001-2005 The Apache Software Foundation

    //

    // Licensed under the Apache License, Version 2.0 (the "License");

    // you may not use this file except in compliance with the License.

    // You may obtain a copy of the License at

    //

    // http://www.apache.org/licenses/LICENSE-2.0

    //

    // Unless required by applicable law or agreed to in writing, software

    // distributed under the License is distributed on an "AS IS" BASIS,

    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

    // See the License for the specific language governing permissions and

    // limitations under the License.

    //

    #endregion

    // We want this assembly to have a seperate logging repository to the

    // rest of the application. We will configure this repository seperatly.

    //设定配置文件文件使用独立配置

    [assembly: log4net.Config.Repository("SimpleModule")]

    // Configure logging for this assembly using the 'SimpleModule.dll.log4net' file

    //配置log4net监视的配置文件后缀为log4net的文件

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

    namespace SimpleModule

    {

    /// <summary>

    /// Summary description for Math.

    /// </summary>

    public class Math

    {

    // Create a logger for use in this class

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public Math()

    {

    if (log.IsDebugEnabled) log.Debug("Constructor");

    }

    public int Add(int left, int right)

    {

    int result = left + right;

    if (log.IsInfoEnabled) log.Info("" + left + " + " + right + " = " + result);

    return result;

    }

    }

    }

    SimpleModule.dll.log4net 配置

    <?xml version="1.0" encoding="utf-8" ?>

    <!-- This section contains the log4net configuration settings -->

    <log4net>

    <!-- Define some output appenders -->

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >

    <!--<file value="SimpleModule.txt" />-->

    <layout type="log4net.Layout.PatternLayout">

    <conversionPattern value="[hbb0b0SimpleModuleConfig] %date [%thread] %-5level %logger - %message%newline" />

    </layout>

    </appender>

    <!-- Setup the root category, add the appenders and set the default priority -->

    <root>

    <level value="DEBUG" />

    <appender-ref ref="ConsoleAppender" />

    </root>

    </log4net>

    程序输出

    clip_image004

  • 相关阅读:
    C# 时间+三位随机数
    dataGridView加行标识方法与制作
    MySql多列查询
    php 去掉字符串的最后一个字符
    DataTable 排序
    汇编第一个程序 Hello World (初学者与入门)
    C# 获取前一天,明天,本周,上周,本季度等!
    php 字符串中任意添加
    天涯论坛的经典回帖!!!
    系统时间同步
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/2174034.html
Copyright © 2020-2023  润新知