Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
<configSections>
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
 </configSections>


XML
<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>


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

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


Loggers.cs:
C#
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);
        }

C#
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:
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
Posted
Updated 5-Jan-15 19:02pm
v5
Comments
CHill60 5-Jan-15 8:42am    
Not clear. More detail required - how are you producing this log?
sridharan.N 6-Jan-15 0:35am    
Thanks for th reply and i will improved my question sir.
ZurdoDev 5-Jan-15 9:01am    
What exactly is your question?
sridharan.N 6-Jan-15 0:35am    
Thanks for th reply and i will improved my question sir.
CHill60 5-Jan-15 9:19am    
We can't see your screen or your HDD - *how* are you logging this (not what are you using to log this). How have you configured log4net, how are you calling it?

1 solution

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



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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900