在WCF中记录错误 - 抛出异常时,没有任何内容存储在日志文件中 [英] Logging errors in WCF - Nothing being stored in log file when exception is being thrown
问题描述
我一直在尝试在WCF服务中捕获异常,并将它们存储在一个日志文件中(我不希望客户端在这一点上得到任何特定的错误消息)。
在线阅读了几本教程后,我已经将一些日志配置添加到web.config的< system.diagnostics>
部分:
< sources>
< source name =System.ServiceModel.MessageLoggingswitchValue =Error>
< listeners>
< add type =System.Diagnostics.DefaultTraceListenername =Default>
< filter type =/>
< / add>
< add name =ServiceModelMessageLoggingListener>
< filter type =/>
< / add>
< / listeners>
< / source>
< source name =System.ServiceModelswitchValue =ErrorpropagateActivity =true>
< listeners>
< add type =System.Diagnostics.DefaultTraceListenername =Default>
< filter type =/>
< / add>
< add name =ServiceModelTraceListener>
< filter type =/>
< / add>
< / listeners>
< / source>
< / sources>
< sharedListeners>
< add initializeData =C:\inetpub\wwwroot\myApp\logs\Web_messages.svclogtype =System.Diagnostics.XmlWriterTraceListener,System,Version = 4.0.0.0,Culture = neutral ,PublicKeyToken = b77a5c561934e089name =ServiceModelMessageLoggingListenertraceOutputOptions =Timestamp>
< filter type =/>
< / add>
< add initializeData =C:\inetpub\wwwroot\myApp\logs\Web_tracelog.svclogtype =System.Diagnostics.XmlWriterTraceListener,System,Version = 4.0.0.0,Culture = neutral ,PublicKeyToken = b77a5c561934e089name =ServiceModelTraceListenertraceOutputOptions =Timestamp>
< filter type =/>
< / add>
< / sharedListeners>
如果发生异常,我已尝试使用Debug.WriteLine和Trace.WriteLine,但没有正在写入tracefile。
我知道IIS / WCF可以写入日志文件,如果我将任何一个源的switchValue设置为更详细我会收到的信息(太多无法使用),但我只想要异常代码。
尝试这个,
考虑以下类
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,参数);
}
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 =xyzswitchName =AllSwitch>
< listeners>
< add name =xyzListenertype =System.Diagnostics.TextWriterTraceListenerinitializeData =C:\Traces\xyz.txttraceOutputOptions =DateTime/>
< / listeners>
< / source>
< / sources>
<开关>
< add name =AllSwitchvalue =全部/>
< add name =OnlyErrorsvalue =Error/>
< / switches>
< trace autoflush =trueindentsize =3/>
< /system.diagnostics>
最后,为了使用它:
TraceUtilitiy trcSrc = new TraceUtility(xyz);
trcSrc.TraceError(Error:{0},您的错误说明);
请注意,您可以像跟踪实用程序类一样向跟踪实用程序类添加跟踪实用程序类的跟踪方法追踪错误方法。
希望这是你正在寻找的。 p>
编辑: / strong>
您的其他代码没有运行,因为您没有为System.Diagnostics.Trace类指定自定义侦听器。为此,请将以下部分添加到您的配置中:
< system.diagnostics>
< trace autoflush =trueindentsize =4>
< listeners>
< remove name =DefaultTraceListener/>
< add name =LogFileListenertype =System.Diagnostics.TextWriterTraceListenerinitializeData =C:\Traces\xyz1.txt/>
< / listeners>
< / trace>
< /system.diagnostics>
,并尝试使用旧的代码。希望这样会让你失望;)
I've been trying to catch exceptions in my WCF service and store them in a log file (I don't want the client to get any specific error message at this point).
Having read a few tutorials online I've added some logging configuration to the <system.diagnostics>
section of web.config:
<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>
In the event of an exception I've been tried using Debug.WriteLine and Trace.WriteLine but nothing is getting written to the tracefile.
I know IIS / WCF can write to the log file, if I set the switchValue for either of the sources to be Verbose I get plenty of information (too much to be of use), but I only want the exception code.
Try this, consider the following 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);
}
}
Now add the following config section to your configuration file
<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>
Finally, in order to use it:
TraceUtilitiy trcSrc = new TraceUtility("xyz");
trcSrc.TraceError("Error: {0}", "Your error description");
Note that you can add trace methods for information and warnings to the trace utility class just as I did with the trace error methods.
Hope this is what you are seeking for.
EDIT: Your other code did not run because you did not specify a custom listener to the System.Diagnostics.Trace class. To do so add the following section into your config:
<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>
and try using your old code. Hope this will de-baffle you ;)
这篇关于在WCF中记录错误 - 抛出异常时,没有任何内容存储在日志文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!