سؤال

I've got a ClickOnce (C#) application that uses log4net UdpAppender to log to a service using XmlLayoutSchemaLog4j. Problem is the log contains "DefaultDomain" as log4japp instead of the real application name.

My UDP appender:

<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
  <param name="RemoteAddress" value="127.0.0.1" />
  <param name="RemotePort" value="62215" />
  <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
    <locationInfo value="true"/>
  </layout>
</appender>

Output:

<xml xmlns:log4j="http://jakarta.apache.org/log4j/">
  <log4j:event logger="MainWindow" timestamp="1400068443197" level="ERROR" thread="4">
    <receivedTimestamp>2014-05-14 11:54:07.459</receivedTimestamp>
    <log4j:message>Oh noes! (1)</log4j:message>
    <log4j:properties>
      <log4j:data name="log4japp" value="DefaultDomain" />
      <log4j:data name="log4net:UserName" value="NML\baltzersen" />
      <log4j:data name="log4jmachinename" value="lgx-Paw" />
      <log4j:data name="log4net:HostName" value="lgx-Paw" />
    </log4j:properties>
    <log4j:throwable>System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at Log4NetTestApp.MainWindow.&lt;bError_Click&gt;b__0(Object str)</log4j:throwable>
    <log4j:locationInfo class="Common.Logs" method="WriteLog" file="" line="0" />
  </log4j:event>
</xml>

How do I make the log4japp element contain my application name?

EDIT: My logger class (LogB) has a static method for getting the logger:

public static ILogB GetLogger(Type type)
{
  return new LogB(Logs.GetLogger(type));
}

Logs.GetLogger:

public static ILog GetLogger(Type 
{
  return LogManager.GetLogger(type.Name);
}

And the constructor for LogB:

if (initiated)
{
  this.logger = logger;
  return;
}
if (File.Exists(log4netFile))
{
  FileInfo fileInfo = new FileInfo(log4netFile);
  XmlConfigurator.ConfigureAndWatch(fileInfo);
  initiated = true;
}
هل كانت مفيدة؟

المحلول

I managed to fix this myself with an appender that inherits UdpAppender:

public class MyUdpAppender : UdpAppender
{
  private readonly string app = Process.GetCurrentProcess().MainModule.FileName.Split('\\').Last().Replace(".exe", "");
  protected override bool FilterEvent(LoggingEvent loggingEvent)
  {
    loggingEvent.GetProperties()["log4japp"] = this.app;
    return base.FilterEvent(loggingEvent);
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top