将服务跟踪查看器中的ApplicationData格式化为XML

时间:2011-10-10 20:37:03

标签: c# .net svcutil.exe tracesource

我正在使用TraceSource将信息记录到XmlWriterTraceListener。我正在记录的消息是XML,但是,当我在Service Trace Viewer中查看消息时,它不会显示为XML,而是显示为字符串。有没有办法做到这一点? 这是我的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="system.framework.db.utility" switchName="switchInformation">
        <listeners>
          <remove name="Default" />
          <add name="arquivoXml" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="switchErro" value="Error"/>
      <add name="switchInformation" value="Information"/>
    </switches>
    <sharedListeners>
      <add name="arquivoXml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="C:\Temp\trace.svclog">
      </add>
    </sharedListeners>
  </system.diagnostics>
</configuration>

以下是我的代码:

namespace system.framework.db.utility.sqlserver
{
  internal class SqlDBTransManager : IDBManagerConnection
  {
    private static readonly TraceSource ts = new TraceSource("system.framework.db.utility");

    private void RunSqlInternal(String pSql, DBManagerParams pDBManagerParams, DBManagerConnection pTransac)
    {
      //Lots of code, and below is the log
      StringBuilder sb = new StringBuilder(1000);
      XmlWriterSettings settings = new XmlWriterSettings();
      settings.ConformanceLevel = ConformanceLevel.Document;
      using (XmlWriter xml = XmlWriter.Create(sb, settings))
      {
        xml.WriteStartDocument(true);
        xml.WriteStartElement("log");
        xml.WriteAttributeString("Método", "RunSql");
        xml.WriteString(pSql);
        xml.WriteEndElement();
        xml.WriteEndDocument();
        xml.Flush();
      }
      ts.TraceEvent(TraceEventType.Information, 1, sb.ToString());

      oCommand.ExecuteNonQuery();
    }
  }
}

以下是它在Service Trace Viewer中的显示方式

enter image description here

无论如何,<ApplicationData>标记下的内容是否格式化为XML?

修改

我打开了svcfile,我看到字符串编码不正确。为什么不呢?

<ApplicationData>&lt;log Método=&quot;RunSql&quot;&gt;drop procedure dbo.spfwug_in_controle_versao&lt;/log&gt;</ApplicationData>

2 个答案:

答案 0 :(得分:1)

无需使用企业库混乱代码;只需使用TraceSource的TraceData()方法,将XPathNavigator作为对象参数传递:

TextReader reader = new StringReader(message);
var xml = new XPathDocument(reader).CreateNavigator();
this.traceSource.TraceData(TraceEventType.Information, -2, xml);

答案 1 :(得分:0)

我能够执行此转储TraceSource并使用Enterprise Library 5.0。这是XmlLogEntry解决了我的问题。以下是代码:

  internal class SqlDBTransManager : IDBManagerConnection
  {
    private void RunSqlInternal(String pSql, DBManagerParams pDBManagerParams, DBManagerConnection pTransac)
    {
      ////Lots of code, and below is the log
      XmlDocument doc = new XmlDocument();
      XPathNavigator nav = doc.CreateNavigator();
      using (XmlWriter xml = nav.AppendChild())
      {
        xml.WriteStartElement("log");
        xml.WriteAttributeString("Método", "RunSql");
        xml.WriteString(pSql);
        xml.WriteEndElement();
        xml.Flush();
      }
      XmlLogEntry entry = new XmlLogEntry();
      entry.Xml = nav;
      entry.Priority = 1;
      entry.Categories = new String[] { "DB" };
      entry.Severity = TraceEventType.Information;
      Logger.Write(entry);

      oCommand.ExecuteNonQuery();
    }
  }

之后,我在web.config中配置了一个XML Trace Listener:

<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="System.ServiceModel" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        fileName="c:\\temp\\trace_framework.svclog" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId" />
    </listeners>
    <categorySources>
      <add switchValue="All" name="System.ServiceModel">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

在此之后,我发送的XML被正确格式化为svclog格式的XML。