Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何筛选/禁用WCF跟踪中记录的异常? 有没有办法防止在WCF跟踪中记录某种类型的异常 更好的是,有没有办法防止记录一个特定的异常(我的意思是在特定代码行抛出一个异常)_C#_Wcf_Exception_Trace - Fatal编程技术网

C# 如何筛选/禁用WCF跟踪中记录的异常? 有没有办法防止在WCF跟踪中记录某种类型的异常 更好的是,有没有办法防止记录一个特定的异常(我的意思是在特定代码行抛出一个异常)

C# 如何筛选/禁用WCF跟踪中记录的异常? 有没有办法防止在WCF跟踪中记录某种类型的异常 更好的是,有没有办法防止记录一个特定的异常(我的意思是在特定代码行抛出一个异常),c#,wcf,exception,trace,C#,Wcf,Exception,Trace,解释: 我正在使用两个Windows服务(A)和(B)。在服务(A)中,我在app.config文件中启用了WCF跟踪,如下所示: <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true"> <listene

解释:

我正在使用两个Windows服务(A)和(B)。在服务(A)中,我在app.config文件中启用了WCF跟踪,如下所示:

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel" switchValue="Warning" 
                propagateActivity="true">
            <listeners>
                <add name="traceListener" 
                     type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="C:\WCF-log.txt" />
            </listeners>
        </source>
    </sources>
</system.diagnostics>

现在,服务(B)有时会停止,这是正常的。我希望这些引发的异常不会记录在WCF跟踪中,因为我的应用程序已经对它们进行了处理,并且我希望只有在出现“真实”问题时才能查看C:\WCF-log.txt文件。

您应该在跟踪侦听器配置中添加过滤器类型,如下所示

<system.diagnostics>
        <sources>
            <source name="System.ServiceModel" switchValue="Warning"
                    propagateActivity="true">
                <listeners >
                    <add name="traceListener"
                         type="System.Diagnostics.XmlWriterTraceListener"
                         initializeData="C:\WCF-log.txt" >
                        <filter type="FilterExceptions"/>
                    </add>                  
                </listeners>
            </source>
        </sources>
    </system.diagnostics>

是的,您可以编写自定义筛选器,例如,在筛选器中,您可以尝试查找跟踪中的内容(如堆栈跟踪中的值或特定错误),以过滤掉该值。但是我没有时间写一个有效的例子。@MatthewMartin谢谢,Milan在下面提供了一个代码示例。谢谢,这正是我想要的!我只是添加以下内容来编写筛选器:
,否则我的代码引发了一个异常“System.ServiceModel.Diagnostics.TraceUtibility的类型初始值设定项引发了一个异常”,其内部异常是“找不到类FilterExceptions的类型”。
<system.diagnostics>
        <sources>
            <source name="System.ServiceModel" switchValue="Warning"
                    propagateActivity="true">
                <listeners >
                    <add name="traceListener"
                         type="System.Diagnostics.XmlWriterTraceListener"
                         initializeData="C:\WCF-log.txt" >
                        <filter type="FilterExceptions"/>
                    </add>                  
                </listeners>
            </source>
        </sources>
    </system.diagnostics>
 public class FilterExceptions : TraceFilter
    {
        public override bool ShouldTrace(TraceEventCache cache, string source, TraceEventType eventType, int id, string formatOrMessage, object[] args, object data1, object[] data)
        {
            // Add exception filter in below if when event type is error
            if (eventType == TraceEventType.Error)
            {
                return false;
            }

            return true;
        }
    }