我一直试图在WCF服务中捕获异常并将它们存储在日志文件中(我现在不希望客户端收到任何特定的错误消息)。
在线阅读了一些教程之后,我在web.config的<system.diagnostics>
部分添加了一些日志记录配置:
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Error">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Error" propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\inetpub\wwwroot\myApp\logs\Web_messages.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="C:\inetpub\wwwroot\myApp\logs\Web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
在发生异常的情况下,我已经尝试使用Debug.WriteLine和Trace.WriteLine,但是没有任何内容写入跟踪文件。
我知道IIS/WCF可以写入日志文件,如果我将任一源的switchValue设置为Verbose,我将获得大量信息(太多用处),但是我只想要异常代码。
最佳答案
尝试这个,
考虑以下类(class)
public class TraceUtility
{
private TraceSource trcSrc;
public TraceUtility(string traceSourceName)
{
if (string.IsNullOrEmpty(traceSourceName))
{
throw new ArgumentNullException(traceSourceName);
}
trcSrc = new TraceSource(traceSourceName);
}
public void TraceError(int id, string message)
{
trcSrc.TraceEvent(TraceEventType.Error, id, message);
}
public void TraceError(int id, string message, params object[] args)
{
trcSrc.TraceEvent(TraceEventType.Error, id, message, args);
}
public void TraceError(string message)
{
TraceError(0, message);
}
public void TraceError(string message, params object[] args)
{
TraceError(0, message, args);
}
}
现在将以下配置部分添加到您的配置文件中
<system.diagnostics>
<sources>
<source name="xyz" switchName="AllSwitch">
<listeners>
<add name="xyzListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Traces\xyz.txt" traceOutputOptions="DateTime" />
</listeners>
</source>
</sources>
<switches>
<add name="AllSwitch" value="All"/>
<add name="OnlyErrors" value="Error"/>
</switches>
<trace autoflush="true" indentsize="3"/>
</system.diagnostics>
最后,为了使用它:
TraceUtilitiy trcSrc = new TraceUtility("xyz");
trcSrc.TraceError("Error: {0}", "Your error description");
请注意,您可以将信息和警告的跟踪方法添加到跟踪实用程序类中,就像我对跟踪错误方法所做的那样。
希望这是您想要的。
编辑:
您的其他代码未运行,因为您没有为System.Diagnostics.Trace类指定自定义侦听器。为此,将以下部分添加到您的配置中:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<remove name="DefaultTraceListener" />
<add name="LogFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Traces\xyz1.txt" />
</listeners>
</trace>
</system.diagnostics>
并尝试使用您的旧代码。希望这会给您带来困惑;)
关于wcf - WCF中的日志记录错误-引发异常时,日志文件中不存储任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569547/