如何启用WCF日志记录,以便它写入数据库? [英] How can I enable WCF logging so that it writes to a Database?

查看:628
本文介绍了如何启用WCF日志记录,以便它写入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够在数据库中记录消息信息,我想决定如何做到最好。是否可以将WCF日志记录机制配置为写入数据库而不是文件?感谢。

解决方案

您需要有两件事:




  • 启用.NET跟踪的正确配置

  • 跟踪侦听器捕获跟踪消息并将其存储在数据库中



对于#1

您需要先在WCF中启用跟踪 - 您需要在< system.serviceModel> 启用跟踪:

 < system.serviceModel> 
< diagnostics>
< messageLogging
logEntireMessage =true
logMalformedMessages =false
logMessagesAtServiceLevel =true
logMessagesAtTransportLevel =false
maxMessagesToLog = 300000
maxSizeOfMessageToLog =200000/>
< / diagnostics>
< /system.serviceModel>接下来,您需要配置.NET跟踪:








$ b < b $ b

 < system.diagnostics> 
< sources>
< source name =System.ServiceModel
switchValue =Information,ActivityTracing
propagateActivity =true>
< listeners>
< add name =xml/>
< / listeners>
< / source>
< source name =System.ServiceModel.MessageLogging>
< listeners>
< add name =xml/>
< / listeners>
< / source>
< / sources>
< sharedListeners>
< add name =xmltype =WcfTrace.Trace.WebTraceListener,WcfTrace.Trace/>
< / sharedListeners>
< /system.diagnostics>

这里,代替WebTraceListener或其他预定义的侦听器,

对于#2:

当然,您可以编写自己的 SqlTraceListener - 或者你可以使用许多现成的解决方案之一,例如此处显示的代码(从 Codeplex )。


I'd like to be able to log message information in a database, and I'm trying to decide how best to do that. Is it possible to configure the WCF logging mechanisms to write to a database instead of a file? Thanks.

解决方案

You need to have two things:

  • a proper config to enable .NET tracing
  • a trace listener to capture the trace messages and store them in a database

For #1:
You need to turn on tracing in WCF first - you need an entry in <system.serviceModel> that enables tracing:

<system.serviceModel>
   <diagnostics>
      <messageLogging
          logEntireMessage="true"
          logMalformedMessages="false"
          logMessagesAtServiceLevel="true"
          logMessagesAtTransportLevel="false"
          maxMessagesToLog="300000"
          maxSizeOfMessageToLog="200000"/>
    </diagnostics>
</system.serviceModel>

Next, you need to configure .NET tracing as such:

<system.diagnostics>
   <sources>
      <source name="System.ServiceModel" 
              switchValue="Information, ActivityTracing" 
              propagateActivity="true">
         <listeners>
            <add name="xml" />
         </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
         <listeners>
             <add name="xml" />
         </listeners>
      </source>
   </sources>
   <sharedListeners>
       <add name="xml" type="WcfTrace.Trace.WebTraceListener,WcfTrace.Trace" />
   </sharedListeners>
</system.diagnostics>

Here, instead of the WebTraceListener, or other pre-defined listeners, you can also plug in your own database-oriented trace listener.

For #2:
You can - of course - write your own SqlTraceListener - or you can use one of the many ready-made solutions out there, for instance this one here (download the code from Codeplex).

这篇关于如何启用WCF日志记录,以便它写入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆