log4net: Custom appender not being invoked

  • Thread starter Thread starter rh.krish
  • Start date Start date
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.
 
Hi,

Why do youn not simple do what is stated on that page?

Paul Bunyan supports virtually all languages, environments, and application
types and is easily integrated into your projects in less than 10 minutes!
Putting out fires and pressed for time? Call our support staff and they'll
get you up and running immediately!

Simplify life and start enjoying Saturdays off! Do yourself a favor and
check out Paul Bunyan - the best damn logger of all time!

Cor


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.
 
I believe the request is not getting landed to Paul Bunyan since
invoking the appender is log4net's job. If it's Paul Bunyab's issue, I
would definitely get their support. I'm sure it is something to do
with the log4net.

I tried to write the log entries using StreamWriter.WriteLine and I
found that the file is being created with 0 bytes. I create the file
in the Log4netPaulBunyanAppender type's constructor and write content
into the file in the Append method. But this method doesn't seem to be
called, though the file was created as I stated above. I'm confused
with this because, it looks like the log4net invokes the appender, but
not the Append method which actually logs the message.
 
I enabled log4net internal debugging and redirected the output to a
text file using trace listener. And I see an error message now:

log4net:ERROR [Log4netPaulBunyanAppender] Failed in DoAppend
System.IO.FileNotFoundException: The specified module could not be
found. (Exception from HRESULT: 0x8007007E)
at Pps.EnterpriseLibrary.Logging.Log4netPaulBunyanAppender.Append
(LoggingEvent loggingEvent)
at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent
loggingEvent)

My appender is being invoked, but it's trying to find some dll, and it
could not and throws an exception. I used Red Gate's .NET Reflector
and ildasm to check the assembly and I couldn't find anything missing.
I have all the DLL's in the GAC and I also tried removing them and
linking them privately. Can somebody help me in this?


log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral,
PublicKeyToken=1b44e1d426115821]. Loaded from [Global Assembly Cache].
(.NET Runtime [2.0.50727.42] on Microsoft Windows NT 5.2.3790 Service
Pack 2)
log4net: DefaultRepositorySelector: defaultRepositoryType
[log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly
[Pps.EquipmentTransfer.WebUI, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly
[Pps.EquipmentTransfer.WebUI, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null] Loaded From [C:\WINDOWS\Microsoft.NET\Framework
\v2.0.50727\Temporary ASP.NET Files\et\ac26e61a\1beceee5\assembly
\dl3\f3f53f44\e737e693_0997c901\Pps.EquipmentTransfer.WebUI.DLL]
log4net: DefaultRepositorySelector: Assembly
[Pps.EquipmentTransfer.WebUI, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly
[Pps.EquipmentTransfer.WebUI, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null] using repository [log4net-default-repository] and
repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-
default-repository] using type
[log4net.Repository.Hierarchy.Hierarchy]
log4net: XmlConfigurator: configuring repository [log4net-default-
repository] using file [D:\Inetpub\wwwroot\ET
\Pps.EquipmentTransfer.WebUI.log4net.config] watching for file updates
log4net: XmlConfigurator: configuring repository [log4net-default-
repository] using file [D:\Inetpub\wwwroot\ET
\Pps.EquipmentTransfer.WebUI.log4net.config]
log4net: XmlConfigurator: configuring repository [log4net-default-
repository] using stream
log4net: XmlConfigurator: loading XML configuration
log4net: XmlConfigurator: Configuring Repository [log4net-default-
repository]
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is
[DEBUG].
log4net: XmlHierarchyConfigurator: Logger [root] level set to
[name="DEBUG",value=30000].
log4net: XmlHierarchyConfigurator: Loading Appender
[PaulBunyanAppender] type:
[Pps.EnterpriseLibrary.Logging.Log4netPaulBunyanAppender,
Pps.EnterpriseLibrary.Logging, Version=1.0.0.5,
Culture=neutral,PublicKeyToken=63073e399fa578f4]
log4net: DefaultRepositorySelector: Creating repository for assembly
[Pps.EnterpriseLibrary.Logging, Version=1.0.0.5, Culture=neutral,
PublicKeyToken=63073e399fa578f4]
log4net: DefaultRepositorySelector: Assembly
[Pps.EnterpriseLibrary.Logging, Version=1.0.0.5, Culture=neutral,
PublicKeyToken=63073e399fa578f4] Loaded From [Global Assembly Cache]
log4net: DefaultRepositorySelector: Assembly
[Pps.EnterpriseLibrary.Logging, Version=1.0.0.5, Culture=neutral,
PublicKeyToken=63073e399fa578f4] does not have a RepositoryAttribute
specified.
log4net: DefaultRepositorySelector: Assembly
[Pps.EnterpriseLibrary.Logging, Version=1.0.0.5, Culture=neutral,
PublicKeyToken=63073e399fa578f4] using repository [log4net-default-
repository] and repository type
[log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: repository [log4net-default-
repository] already exists, using repository type
[log4net.Repository.Hierarchy.Hierarchy]
log4net: PatternParser: Converter [message] Option [] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property
[ConversionPattern] to String value [[%thread] - %message [line#:
%line]]
log4net: PatternParser: Converter [literal] Option [[] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [thread] Option [] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [] - ] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [message] Option [] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ [line#:] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [line] Option [] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option []] Format
[min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object
[log4net.Layout.PatternLayout]
log4net: XmlHierarchyConfigurator: Created Appender
[PaulBunyanAppender]
log4net: XmlHierarchyConfigurator: Adding appender named
[PaulBunyanAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []
log4net:ERROR [Log4netPaulBunyanAppender] Failed in DoAppend
System.IO.FileNotFoundException: The specified module could not be
found. (Exception from HRESULT: 0x8007007E)
at Pps.EnterpriseLibrary.Logging.Log4netPaulBunyanAppender.Append
(LoggingEvent loggingEvent)
at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent
loggingEvent)
 
Back
Top