• Use Multiple log4net Outputs from One Application


    Introduction

    This is an article simply to demonstrate how to use several output log files depending on the library or the application.

    Using the Code

    Developers who develop class libraries may need to log details separate from the main application to support users or to quickly retrieve the data. But when they try to implement this, they will face a problem. This is because normally log4net has only one config section under the ASP.NET web.config file or Windows application app.config file. To overcome this problem, you can use different logger entries which define the output.

    Default case:

    <log4net>
        <appender type="log4net.Appender.RollingFileAppender" name="RollingFile">
            <file value="c:\logs\Log.txt" />
            <layout type="log4net.Layout.PatternLayout" />
                <conversionpattern value="%d [%t] %-5p %c - %m%n" />
            </layout>
        </appender>
    
        <root>
            <level value="ALL" />
            <appender-ref ref="RollingFile" />
        </root>
    </log4net>

    When you specify the <root /> tag, it says all the log data will log to that output file. To stop this, we need to remove the <root /> element and have separate <logger /> tags for every library or the application. You also need to specify your root namespace for the library.

    <log4net>
        <appender type="log4net.Appender.RollingFileAppender" name="classApp1">
          <file value="c:\Library1.txt" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionpattern value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
    
        <appender type="log4net.Appender.RollingFileAppender" name="classApp2">
          <file value="c:\Library2.txt" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionpattern value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
    
        <appender type="log4net.Appender.RollingFileAppender" name="application">
          <file value="c:\Application.txt" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionpattern value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
    
        <logger name="ClassLibrary1">
          <level value="ERROR" />
          <maximumfilesize value="256KB" />
          <param value="ERROR" name="Threshold" />
    
          <appender-ref ref="classApp1" />
        </logger>
    
        <logger name="ClassLibrary2">
          <level value="WARN" />
          <maximumfilesize value="256KB" />
          <param value="WARN" name="Threshold" />
    
          <appender-ref ref="classApp2" />
        </logger>
    
        <logger name="WindowsApplication1">
          <level value="WARN" />
          <maximumfilesize value="256KB" />
          <param value="WARN" name="Threshold" />
    
          <appender-ref ref="application" />
        </logger>
    
      </log4net>

    You can see three <logger /> items and separate <appender />tags referenced from those loggers. "name" of the <logger /> will specify the root namespace of the library or the application. Specify which appender you need to refer from the logger. When executing the application, if something is written to the log from the ClassLibrary2.Class2, it will refer the <logger name="ClassLibrary2" /> entry. This means the output will be on c:\Library2.txt file. This way you can specify any amount of loggers dynamically.

    Note: The code in Visual Studio 2005 format.

    From:  https://www.codeproject.com/Articles/18720/Use-Multiple-log-net-Outputs-from-One-Application

  • 相关阅读:
    转:IPhone之ASIFormDataRequest POST操作架构设计/ 处理网络超时问题
    LLDB和GDB比较
    为线程设置一个名字 [mythread setName:@"第一个子线程"];
    杀死一个线程
    ios 开发框架原始雏形 01
    iOS开发:设置应用程序图标和引导画面
    一个奇怪的现象 在GDB模式和LLDB 模式下 同样代码不同反应 AudioServicesCreateSystemSoundID
    iOS中GCD的魔力
    提升app 应用程序运行速度的几个常用方法
    IOS开发缓存机制之—本地缓存机制
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9172688.html
Copyright © 2020-2023  润新知