• Log4net配置


    <?xml version="1.0" encoding="utf-8" ?>
    
    <log4net>
    
        <appender name="TastInfo" type="log4net.Appender.RollingFileAppender">
    
            <file value="Log\Info\" />
    
            <appendToFile value="true" />
    
            <rollingStyle value="Composite" />
    
            <maxSizeRollBackups value="-1" />
    
            <maximumFileSize value="1MB" />
    
            <staticLogFileName value="false" />
    
            <DatePattern value="yyyy-MM-dd&quot;.txt&quot;"/>
    
            <layout type="log4net.Layout.PatternLayout">
    
                <conversionPattern value="%date  %-5level  - %message%newline" />
    
            </layout>
    
        </appender>
    
        <appender name="TastError" type="log4net.Appender.RollingFileAppender">
    
            <file value="log\Error\" />
    
            <appendToFile value="true" />
    
            <rollingStyle value="Composite" />
    
            <maxSizeRollBackups value="-1" />
    
            <maximumFileSize value="1MB" />
    
            <staticLogFileName value="false" />
    
            <DatePattern value="yyyy-MM-dd&quot;.txt&quot;"/>
    
            <layout type="log4net.Layout.PatternLayout">
    
                <conversionPattern value="%date  %-5level - %message%newline" />
    
            </layout>
    
        </appender>
    
        <appender name="TastDebug" type="log4net.Appender.RollingFileAppender">
    
            <file value="log\Debug\" />
    
            <appendToFile value="true" />
    
            <rollingStyle value="Composite" />
    
            <maxSizeRollBackups value="-1" />
    
            <maximumFileSize value="1MB" />
    
            <staticLogFileName value="false" />
    
            <DatePattern value="yyyy-MM-dd&quot;.txt&quot;"/>
    
            <layout type="log4net.Layout.PatternLayout">
    
                <conversionPattern value="%date  %-5level - %message%newline" />
    
            </layout>
    
        </appender>
    
        <logger name="Info">
    
            <level value="ALL"/>
    
            <appender-ref ref="Info" />
    
            <appender-ref ref="TastInfo" />
    
        </logger>
    
        <logger name="Error">
    
            <level value="ALL"/>
    
            <appender-ref ref="Error" />
    
            <appender-ref ref="TastError" />
    
        </logger>
    
        <logger name="Debug">
    
            <level value="ALL"/>
    
            <appender-ref ref="Debug" />
    
            <appender-ref ref="TastDebug" />
    
        </logger>
    
    </log4net>
    public class Log
    
        {
    
            private const string SError = "Error";
    
            private const string SDebug = "Debug";
    
            private const string DefaultName = "Info";
    
    
    
            static Log()
    
            {
    
                var path = AppDomain.CurrentDomain.BaseDirectory + @"log4net_config.xml";
    
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
    
            }
    
    
    
            public static log4net.ILog GetLog(string logName)
    
            {
    
                var log = log4net.LogManager.GetLogger(logName);
    
                return log;
    
            }
    
    
    
            public static void Debug(string message)
    
            {
    
                var log = log4net.LogManager.GetLogger(SDebug);
    
                if (log.IsDebugEnabled)
    
                    log.Debug(message);
    
            }
    
    
    
            public static void Debug(string message, Exception ex)
    
            {
    
                var log = log4net.LogManager.GetLogger(SDebug);
    
                if (log.IsDebugEnabled)
    
                    log.Debug(message, ex);
    
            }
    
    
    
            public static void Error(string message)
    
            {
    
                var log = log4net.LogManager.GetLogger(SError);
    
                if (log.IsErrorEnabled)
    
                    log.Error(message);
    
            }
    
    
    
            public static void Error(string message, Exception ex)
    
            {
    
                var log = log4net.LogManager.GetLogger(SError);
    
                if (log.IsErrorEnabled)
    
                    log.Error(message, ex);
    
            }
    
    
    
            public static void Fatal(string message)
    
            {
    
                var log = log4net.LogManager.GetLogger(DefaultName);
    
                if (log.IsFatalEnabled)
    
                    log.Fatal(message);
    
            }
    
    
    
            public static void Info(string message)
    
            {
    
                log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);
    
                if (log.IsInfoEnabled)
    
                    log.Info(message);
    
            }
    
    
    
            public static void Warn(string message)
    
            {
    
                var log = log4net.LogManager.GetLogger(DefaultName);
    
                if (log.IsWarnEnabled)
    
                    log.Warn(message);
    
            } 
    
        }

    不输出日志的原因是因为, 默认private static string DefaultName = "log",在配置文件里面找不到对应的节点值。

    总结:log4net.LogManager.GetLogger(Name),这里面的Name要在配置文件中,有对应的节点值。

    还有就是,假如在web.config里指定了一下信息:
    <!--日志配置文件路径-->
    <add key="log4net" value="\config\log4net_local.config" />

    则务必将在根目录下创建文件夹“config”,并将配置文件log4net_local.config放入config文件夹,这样才确保输出日志文件夹Log。

    转载地址:http://www.cnblogs.com/chendaoyin/archive/2013/04/17/3026717.html

  • 相关阅读:
    riot.js教程【三】访问DOM元素、使用jquery、mount输入参数、riotjs标签的生命周期
    博客园文章编辑器5.0版本发布(markdown版)
    【开源】博客园文章编辑器4.0版发布
    博客园文章编辑器【客户端应用程序】V3.0.0发布
    博客园文章编辑器【客户端应用程序】V2.0.0发布,命名为51cnblogs
    在VSCode中编辑HTML文档时,在Dom标签上写style属性时智能提示的问题
    产品经理做什么?
    riot.js教程【二】组件撰写准则、预处理器、标签样式和装配方法
    riot.js教程【一】简介
    程序员的时间管理哲学——打造自己的分时管理系统
  • 原文地址:https://www.cnblogs.com/zhao123/p/3917176.html
Copyright © 2020-2023  润新知