Problem: I wanted to log different LOG Levels in different files using LOG4Net.
my app.config was like this.
log4net
appender name="LogFileAppender" type="log4net.Appender.FileAppender"
param name="File" value="..\..\Logs\ApplicationLog.log"
param name="AppendToFile" value="false"
layout type="log4net.Layout.PatternLayout"
param name="Header" value=""
param name="Footer" value=""
param name="ConversionPattern" value="%d [%t] %-5p %m%n"
layout
appender
appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"
layout type="log4net.Layout.PatternLayout"
param name="Header" value="[Header]\r\n"
param name="Footer" value="[Footer]\r\n"
param name="ConversionPattern" value="%d [%t] %-5p %m%n"
layout
appender
root
level value="ALL"
appender-ref ref="ConsoleAppender"
appender-ref ref="LogFileAppender"
root
log4net
--
-------------------------------------------------------------------------------------
I added different Appenders for file saving and Logger tag to define my new loggers
(Check TEXT in Green)
-------------------------------------------------------------------------------------
!--
log4net
appender name="LogFileAppender" type="log4net.Appender.FileAppender"
param name="File" value="..\..\Logs\ApplicationLog.log"
param name="AppendToFile" value="false"
layout type="log4net.Layout.PatternLayout"
param name="Header" value=""
param name="Footer" value=""
param name="ConversionPattern" value="%d [%t] %-5p %m%n"
layout
appender
appender name="InputLogFileAppender" type="log4net.Appender.FileAppender"
param name="File" value="..\..\Logs\InputData.log"
param name="AppendToFile" value="false"
layout type="log4net.Layout.PatternLayout"
param name="Header" value=""
param name="Footer" value=""
param name="ConversionPattern" value="%m%n"
layout
appender
appender name="OutputLogFileAppender" type="log4net.Appender.FileAppender"
param name="File" value="..\..\Logs\OutputData.log"
param name="AppendToFile" value="false"
layout type="log4net.Layout.PatternLayout"
param name="Header" value=""
param name="Footer" value=""
param name="ConversionPattern" value="%m%n"
layout
appender
appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"
layout type="log4net.Layout.PatternLayout"
param name="Header" value="[Header]\r\n"
param name="Footer" value="[Footer]\r\n"
param name="ConversionPattern" value="%d [%t] %-5p %m%n"
layout
appender
root
level value="ALL"
appender-ref ref="ConsoleAppender"
root
logger name="LogFileAppender"
level value="DEBUG"
appender-ref ref="LogFileAppender"
logger
logger name="ErrorFileAppender"
level value="ERROR"
appender-ref ref="LogFileAppender"
logger
logger name="InputLogger"
level value="INFO"
appender-ref ref="InputLogFileAppender"
logger
logger name="OutputLogger"
level value="INFO"
appender-ref ref="OutputLogFileAppender"
logger
log4net
--
---------------------------------------------------------------------------------------------
My LOG4Net Level ENUM was look like this before;
---------------------------------------------------------------------------------------------
public enum ELogLevel
{
DEBUG = 1,
ERROR,
FATAL,
INFO,
WARN,
}
---------------------------------------------------------------------------------------------
I added my new LOG4Net Levels (Check Text in green )
---------------------------------------------------------------------------------------------
public enum ELogLevel
{
DEBUG = 1,
ERROR,
FATAL,
INFO,
WARN,
INPUT,
OUTPUT
}
---------------------------------------------------------------------------------------------
My LOG4Net Class was look like this before;
---------------------------------------------------------------------------------------------
public static class CLogger
{
#region Members
private static readonly ILog logger = LogManager.GetLogger(typeof(CLogger));
#endregion
#region Constructors
static CLogger()
{
XmlConfigurator.Configure();
}
#endregion
#region Methods
public static void WriteLog(ELogLevel logLevel, String log)
{
if (logLevel.Equals(ELogLevel.DEBUG))
{
logger.Debug(log);
}
else if (logLevel.Equals(ELogLevel.ERROR))
{
logger.Error(log);
}
else if (logLevel.Equals(ELogLevel.FATAL))
{
logger.Fatal(log);
}
else if (logLevel.Equals(ELogLevel.DEBUG))
{
logger.Info(log);
}
else if (logLevel.Equals(ELogLevel.WARN))
{
logger.Warn(log);
}
else if (logLevel.Equals(ELogLevel.INFO ))
{
logger.Info (log);
}
}
-----------------------------------------------------------------------------------------------
I wrote code for my new LOGGER in config (check text in green)
-----------------------------------------------------------------------------------------------
public static class CLogger
{
#region Members
private static readonly ILog logger = LogManager.GetLogger("LogFileAppender");
private static readonly ILog errorlogger = LogManager.GetLogger("ErrorFileAppender");
private static readonly ILog inputDataLog = LogManager.GetLogger("InputLogger");
private static readonly ILog outputDataLog = LogManager.GetLogger("OutputLogger");
#endregion
#region Constructors
static CLogger()
{
XmlConfigurator.Configure();
}
#endregion
#region Methods
public static void WriteLog(ELogLevel logLevel, String log)
{
if (logLevel.Equals(ELogLevel.DEBUG))
{
logger.Debug(log);
}
else if (logLevel.Equals(ELogLevel.ERROR))
{
errorlogger.Error(log);
}
else if (logLevel.Equals(ELogLevel.FATAL))
{
logger.Fatal(log);
}
else if (logLevel.Equals(ELogLevel.DEBUG))
{
logger.Info(log);
}
else if (logLevel.Equals(ELogLevel.WARN))
{
logger.Warn(log);
}
else if (logLevel.Equals(ELogLevel.INFO ))
{
logger.Info (log);
}
else if (logLevel.Equals(ELogLevel.INPUT ))
{
inputDataLog.Info(log);
}
else if (logLevel.Equals(ELogLevel.OUTPUT ))
{
outputDataLog.Info(log);
}
}
After all this, now i can save my Input and Out level messages to different files and Debug and Error in one file.
Thanks
No comments:
Post a Comment