R
rh.krish
I have written a custom appender to output log messages to Paul Bunyan
Service. (Paul Bunyan is a log viewer and a service from www.diamondsierra.com).
The problem that I have is I'm seeing the logs in Paul Bunyan viewer
in every environment (DEV, TEST, STAGING), except the PROD box. I
don't see any sign of loading this dll. Here is my code:
public class Log4netPaulBunyanAppender : AppenderSkeleton
{
private static ILog log = LogManager.GetLogger
(MethodBase.GetCurrentMethod().DeclaringType);
protected override void Append(log4net.Core.LoggingEvent
loggingEvent)
{
try
{
string context = null;
ThreadContextStack property =
loggingEvent.LookupProperty("NDC") as ThreadContextStack;
if ((property != null) && (property.Count > 0))
context = property.ToString();
string loggerName = loggingEvent.LoggerName;
string renderedMessage = null;
if (this.Layout == null)
renderedMessage = loggingEvent.RenderedMessage;
else
renderedMessage = base.RenderLoggingEvent
(loggingEvent);
if (context == null)
context = string.Empty;
string instanceId = loggingEvent.Properties
["InstanceId"] as string;
if (instanceId != null)
context = string.Format(format, context,
instanceId);
string pbContext = loggingEvent.Properties
["pbcontext"] as string;
if (pbContext != null)
context = string.Format(format, context,
pbContext);
string exceptionString =
loggingEvent.GetExceptionString();
if ((exceptionString != null) &&
(exceptionString.Length > 0))
renderedMessage = string.Format(format,
renderedMessage, exceptionString);
LocationInfo locInfo = null;
int lineNumber = 0;
string fileName = string.Empty;
locInfo = loggingEvent.LocationInformation;
bool isNumber = int.TryParse(locInfo.LineNumber, out
lineNumber);
fileName = locInfo.FileName;
// Log the event.
PBUtil.Log(fileName, ((isNumber) ? (UInt32)
lineNumber : (UInt32)0), loggerName, context, ConvertMessageType
(loggingEvent.Level), "{0}", new object[] { renderedMessage });
}
catch
{
// consume any exception that would arise...
}
}
private static PBMessageType ConvertMessageType(Level level)
{
PBMessageType msgType;
switch (level.Value)
{
case -2147483648:
case 0x2710:
case 0x4e20:
case 0x7530:
msgType = PBMessageType.Verbose;
break;
case 0xc350:
case 0x9c40:
msgType = PBMessageType.Informational;
break;
case 0xea60:
msgType = PBMessageType.Warning;
break;
case 0x11170:
case 0x13880:
case 0x15f90:
case 0x1d4c0:
case 0x186a0:
case 0x1adb0:
msgType = PBMessageType.Error;
break;
case 0x7fffffff:
msgType = PBMessageType.Internal;
break;
default:
msgType = PBMessageType.Error;
break;
}
return msgType;
}
override protected bool RequiresLayout
{
get { return true; }
}
}
I even tried to write to a text file using StreamWriter to see whether
this appender is being called, but I don't see any proof for it. Here
is how I configured in log4net.config file, which I use the following
code to open the config file:
XmlConfigurator.ConfigureAndWatch(new FileInfo(log4netConfigFile));
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="PaulBunyanAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\pb1log.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="PaulBunyanAppender"
type="MyApp.Logging.Log4netPaulBunyanAppender, MyApp.Logging">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] - %message" />
</layout>
</appender>
</log4net>
where MyAppp.Logging.dll is the assembly and
MyApp.Logging.Log4netPaulBunyanAppender is the type.
I'm struggling with this for the past few days and any help would be
much appreciated.
Thanks.
Service. (Paul Bunyan is a log viewer and a service from www.diamondsierra.com).
The problem that I have is I'm seeing the logs in Paul Bunyan viewer
in every environment (DEV, TEST, STAGING), except the PROD box. I
don't see any sign of loading this dll. Here is my code:
public class Log4netPaulBunyanAppender : AppenderSkeleton
{
private static ILog log = LogManager.GetLogger
(MethodBase.GetCurrentMethod().DeclaringType);
protected override void Append(log4net.Core.LoggingEvent
loggingEvent)
{
try
{
string context = null;
ThreadContextStack property =
loggingEvent.LookupProperty("NDC") as ThreadContextStack;
if ((property != null) && (property.Count > 0))
context = property.ToString();
string loggerName = loggingEvent.LoggerName;
string renderedMessage = null;
if (this.Layout == null)
renderedMessage = loggingEvent.RenderedMessage;
else
renderedMessage = base.RenderLoggingEvent
(loggingEvent);
if (context == null)
context = string.Empty;
string instanceId = loggingEvent.Properties
["InstanceId"] as string;
if (instanceId != null)
context = string.Format(format, context,
instanceId);
string pbContext = loggingEvent.Properties
["pbcontext"] as string;
if (pbContext != null)
context = string.Format(format, context,
pbContext);
string exceptionString =
loggingEvent.GetExceptionString();
if ((exceptionString != null) &&
(exceptionString.Length > 0))
renderedMessage = string.Format(format,
renderedMessage, exceptionString);
LocationInfo locInfo = null;
int lineNumber = 0;
string fileName = string.Empty;
locInfo = loggingEvent.LocationInformation;
bool isNumber = int.TryParse(locInfo.LineNumber, out
lineNumber);
fileName = locInfo.FileName;
// Log the event.
PBUtil.Log(fileName, ((isNumber) ? (UInt32)
lineNumber : (UInt32)0), loggerName, context, ConvertMessageType
(loggingEvent.Level), "{0}", new object[] { renderedMessage });
}
catch
{
// consume any exception that would arise...
}
}
private static PBMessageType ConvertMessageType(Level level)
{
PBMessageType msgType;
switch (level.Value)
{
case -2147483648:
case 0x2710:
case 0x4e20:
case 0x7530:
msgType = PBMessageType.Verbose;
break;
case 0xc350:
case 0x9c40:
msgType = PBMessageType.Informational;
break;
case 0xea60:
msgType = PBMessageType.Warning;
break;
case 0x11170:
case 0x13880:
case 0x15f90:
case 0x1d4c0:
case 0x186a0:
case 0x1adb0:
msgType = PBMessageType.Error;
break;
case 0x7fffffff:
msgType = PBMessageType.Internal;
break;
default:
msgType = PBMessageType.Error;
break;
}
return msgType;
}
override protected bool RequiresLayout
{
get { return true; }
}
}
I even tried to write to a text file using StreamWriter to see whether
this appender is being called, but I don't see any proof for it. Here
is how I configured in log4net.config file, which I use the following
code to open the config file:
XmlConfigurator.ConfigureAndWatch(new FileInfo(log4netConfigFile));
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="PaulBunyanAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\pb1log.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="PaulBunyanAppender"
type="MyApp.Logging.Log4netPaulBunyanAppender, MyApp.Logging">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] - %message" />
</layout>
</appender>
</log4net>
where MyAppp.Logging.dll is the assembly and
MyApp.Logging.Log4netPaulBunyanAppender is the type.
I'm struggling with this for the past few days and any help would be
much appreciated.
Thanks.