2011-10-10 88 views
1

我正在使用TraceSource將信息記錄到XmlWriterTraceListener。我正在登錄的消息是XML,但是,當我在Service Trace Viewer中查看消息時,它不會顯示爲XML,而是顯示爲字符串。有沒有辦法做到這一點? 這裏是我的app.config將服務跟蹤查看器中的ApplicationData格式化爲XML

<?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(); 
    } 
    } 
} 

及以下它是如何顯示服務跟蹤查看

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> 

回答

0

我能夠做到這一點傾銷TraceSource,並使用企業庫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(); 
    } 
    } 

在那之後,我配置XML跟蹤偵聽器在web.config:

<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的XML svclog格式。

1

無需使用企業庫混淆您的代碼;只需使用TraceSource的TraceData()方法傳遞一個XPathNavigator作爲對象參數:

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