何时在日志文件中输入两次日志? [英] when the logs are entered two times in logfiles?

查看:61
本文介绍了何时在日志文件中输入两次日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用log4net来记录异常。执行日志记录时,日志文件在日志文件中输入两次(LoggerFile.log)



web.config

Am used the log4net to log the exception. when the logging is performed but the logs are entered two times in logfiles (LoggerFile.log)

web.config

<configSections>
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
 </configSections>







<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="E:\\Logger\\LoggerFile.log"/>
     <!-- <file value="E:\\Logger\\LoggerFile.log" />  -->
      <staticLogFileName value="true" />
      <param name="AppendToFile" value="true"/>
      <maximumFileSize value="3MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern

         value="%date [%thread] %-5level %message%newline%exception %newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="FileAppender"/>
    </root>
    <logger name="AnonymousLog">
      <level value="All" />
      <appender-ref ref="FileAppender" />
    </logger>
  </log4net>





登录.aspx.cs:

我将从登录页面调用这两种方法到Loggers Project中的Loggers.cs





Login.aspx.cs:
I will call to this two methods from Login page to Loggers.cs in Loggers Project

Logger.Initialize();
Logger.Log(LoggingLevel.Error,"Checking Log entered");





Loggers.cs:



Loggers.cs:

Initialize Method:
public static void Initialize(string configFile)
       {
           if (!isInitialized)
           {
               if (!String.IsNullOrEmpty(configFile))
                   XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
               else
                   XmlConfigurator.Configure();
               isInitialized = true;
           }
           else
               throw new LoggingInitializationException("Logging has already been         initialized.");
       }

Log Method:
public static void Log(LoggingLevel loggingLevel, string message)
        {
            Log(loggingLevel, message, null, null);
        }




Another Log method.

public static void Log(LoggingLevel loggingLevel, string message, object loggingProperties,  Exception exception)
       {
           foreach (ILog log in GetLeafLoggers())
               LogBase(log, loggingLevel, message, loggingProperties, exception);
       }

GetLeafLoggers()

private static IEnumerable<ILog> GetLeafLoggers()
        {
            ILog[] allLogs = LogManager.GetCurrentLoggers();
            IList<ILog> leafLogs = new List<ILog>();
            for (int i = 0; i < allLogs.Length; i++)
            {
                bool isParent = false;
                for (int j = 0; j < allLogs.Length; j++)
                {
                    if (i != j && allLogs[j].Logger.Name.StartsWith(string.Format("{0}.", allLogs[i].Logger.Name)))
                    {
                        isParent = true;
                        break;
                    }
                }
                if (!isParent)
                    leafLogs.Add(allLogs[i]);
            }
            return leafLogs;
        }

LogBase Method:

private static void LogBase(ILog log, LoggingLevel loggingLevel, string message, object loggingProperties, Exception exception)
        {
            if (ShouldLog(log, loggingLevel))
            {
               // When the Properties null both 
              //properties exit from the conditions.
               // PushLoggingProperties(loggingProperties);
                switch (loggingLevel)
                {
                    case LoggingLevel.Debug: log.Debug(message, exception); break;
                    case LoggingLevel.Info: log.Info(message, exception); break;
                    case LoggingLevel.Warning: log.Warn(message, exception); break;
                    case LoggingLevel.Error: log.Error(message, exception); break;
                    case LoggingLevel.Fatal: log.Fatal(message, exception); break;
                }
               // PopLoggingProperties(loggingProperties);
            }
        }



LoggerFile.log:

日志在日志文件中输入两次



2015-01-05 07:48:16,023 [21]错误检查日志输入

2015-01-05 07:48:16,023 [21]错误检查日志输入


LoggerFile.log:
The Logs are entered two times in the log files

2015-01-05 07:48:16,023 [21] ERROR Checking Log entered
2015-01-05 07:48:16,023 [21] ERROR Checking Log entered

推荐答案

<root>
      <level value="ALL"/>
      <appender-ref ref="FileAppender"/>
    </root>
    <logger name="AnonymousLog">
      <level value="All" />
      <appender-ref ref="FileAppender" />
    </logger>







在web.config文件中给出文件appender两次。所以只有两次写入日志。




In a web.config file two times given the file appender. so thats only a logs are wrote in two times.


这篇关于何时在日志文件中输入两次日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆