我的Log4Net配置如下所示:
<appender name="MainLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="${log4net_log_file_name}"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="1"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true" />
</layout>
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" >
<onlyFixPartialEventData value="true" />
</appender>
</log4net>
我们正在记录这样的例外情况:
private readonly ILog _log = LogManager.GetLogger(typeof(LastChanceHandler));
private void LogException(Exception ex)
{
_log.Fatal("Unhandled exception", ex);
}
当我们记录错误时,我从未在日志中看到Throwable节点。使用当前布局是因为我们将XML转换为DTO并在UI中显示它们。
我没有找到任何解释为什么Throwable没有被设置并存储在日志中。任何想法(我正在考虑让我的开发盒扔掉。窗外)?
这是我日志中的错误:
<log4j:event logger="Hsbc.Ice.Shell.ViewModels.ShellViewModel" timestamp="1353076754456" level="ERROR" thread="1"><log4j:message>14 modules did not terminated in a timely manner : TradingCockpitModule,AuthenticationModule,ConfigurationModule,EntitlementsModule,Hsbc.Ice.CreditServices,EncryptionModule,ReportsModule,DiagnosticsModule,InterProcessCommunication,Layout,Logging,PersistenceModule,StorageModule,ICEExplorerManagerServiceModule</log4j:message><log4j:properties><log4j:data name="log4net:UserName" value="HBEU\steveget" /><log4j:data name="log4net:Identity" value="HBEU\steveget" /><log4j:data name="log4jmachinename" value="E8262XDZW4LZKEC" /><log4j:data name="log4japp" value="Hsbc.Ice.Shell.exe" /><log4j:data name="log4net:HostName" value="E8262XDZW4LZKEC" /></log4j:properties><log4j:locationInfo class="Hsbc.Ice.Shell.ViewModels.ShellViewModel" method="<.ctor>b__3" file="d:\CreditFlow\Tools\Hudson\data\jobs\Credit Shell\workspace\src\Shell\Hsbc.Ice.Shell\ViewModels\ShellViewModel.cs" line="107" /></log4j:event>
答案 0 :(得分:1)
throwable:用于输出已绑定到的Throwable跟踪 LoggingEvent,默认情况下会输出完整的跟踪 通常通过调用Throwable.printStackTrace()找到。
Throwable是a Java class因此在.Net中不可用:也许可以替换异常堆栈跟踪,但它尚未实现。
答案 1 :(得分:1)
只需更换;
private void LogException(Exception ex)
{
_log.Fatal("Unhandled exception", ex);
}
用;
private void LogException(Exception ex)
{
_log.Fatal("Unhandled exception:" + ex.ToString(), ex);
}
您还可以使用ex.StackTrace()
获取信息。
如果您想在.NET中使用整个堆栈跟踪
在.NET中没有可抛出的配置(或者至少我在文档中找不到任何关于它的东西),搜索只产生有意义的Java结果,因为在C#中没有throwable
答案 2 :(得分:0)
在完成答案之后,我决定尝试一些基于Stuart Dunkeld的答案(上面给他答案,我已经把它放在这里以求完整性)。
基本上,我创建了一个从现有布局xml格式化程序继承并覆盖的自定义布局:
重写方法中的重要部分是:
if (exceptionString!=null && !string.IsNullOrEmpty(exceptionString.StackTrace))
{
writer.WriteStartElement("log4j:throwable");
Transform.WriteEscapedXmlString(writer, exceptionString.StackTrace, InvalidCharReplacement);
writer.WriteEndElement();
}
为了完整,这是全班。这允许我们捕获错误的堆栈跟踪以及它的消息:
public class Log4NetErrorLogger : XmlLayoutSchemaLog4j
{
private static readonly DateTime Date1970 = new DateTime(1970, 1, 1);
protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
{
if (loggingEvent.LookupProperty("log4net:HostName") != null &&
loggingEvent.LookupProperty("log4jmachinename") == null)
{
loggingEvent.GetProperties()["log4jmachinename"] = loggingEvent.LookupProperty("log4net:HostName");
}
if (loggingEvent.LookupProperty("log4japp") == null && !string.IsNullOrEmpty(loggingEvent.Domain))
{
loggingEvent.GetProperties()["log4japp"] = loggingEvent.Domain;
}
if (!string.IsNullOrEmpty(loggingEvent.Identity) &&
loggingEvent.LookupProperty("log4net:Identity") == null)
{
loggingEvent.GetProperties()["log4net:Identity"] = loggingEvent.Identity;
}
if (!string.IsNullOrEmpty(loggingEvent.UserName) &&
loggingEvent.LookupProperty("log4net:UserName") == null)
{
loggingEvent.GetProperties()["log4net:UserName"] = loggingEvent.UserName;
}
writer.WriteStartElement("log4j:event");
writer.WriteAttributeString("logger", loggingEvent.LoggerName);
writer.WriteAttributeString("timestamp",
XmlConvert.ToString(
(long)
(loggingEvent.TimeStamp.ToUniversalTime() - Date1970)
.TotalMilliseconds));
writer.WriteAttributeString("level", loggingEvent.Level.DisplayName);
writer.WriteAttributeString("thread", loggingEvent.ThreadName);
writer.WriteStartElement("log4j:message");
Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage, InvalidCharReplacement);
writer.WriteEndElement();
object obj = loggingEvent.LookupProperty("NDC");
if (obj != null)
{
string text = loggingEvent.Repository.RendererMap.FindAndRender(obj);
if (!string.IsNullOrEmpty(text))
{
writer.WriteStartElement("log4j:NDC");
Transform.WriteEscapedXmlString(writer, text, InvalidCharReplacement);
writer.WriteEndElement();
}
}
PropertiesDictionary properties = loggingEvent.GetProperties();
if (properties.Count > 0)
{
writer.WriteStartElement("log4j:properties");
foreach (DictionaryEntry dictionaryEntry in properties)
{
writer.WriteStartElement("log4j:data");
writer.WriteAttributeString("name", (string) dictionaryEntry.Key);
string text = loggingEvent.Repository.RendererMap.FindAndRender(dictionaryEntry.Value);
writer.WriteAttributeString("value", text);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
var exceptionString = loggingEvent.ExceptionObject;
if (exceptionString!=null && !string.IsNullOrEmpty(exceptionString.StackTrace))
{
writer.WriteStartElement("log4j:throwable");
Transform.WriteEscapedXmlString(writer, exceptionString.StackTrace, InvalidCharReplacement);
writer.WriteEndElement();
}
if (LocationInfo)
{
LocationInfo locationInformation = loggingEvent.LocationInformation;
writer.WriteStartElement("log4j:locationInfo");
writer.WriteAttributeString("class", locationInformation.ClassName);
writer.WriteAttributeString("method", locationInformation.MethodName);
writer.WriteAttributeString("file", locationInformation.FileName);
writer.WriteAttributeString("line", locationInformation.LineNumber);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}