2011-10-10 2 views
1

에 정보를 기록하려면 TraceSource을 사용하고 있습니다. 내가 로깅하는 메시지는 XML이지만 서비스 추적 뷰어에서 메시지를 볼 때 XML로 표시되지 않고 문자열로 표시됩니다. 이것을 할 수있는 방법이 있습니까? 이 때문에 어쨌든 서비스 추적 뷰어XML로 Service Trace Viewer의 ApplicationData 형식을 지정하십시오.

enter image description here

에 있나요 보여주는 어떻게

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

그리고 다음과 같습니다 : 가 여기 내의 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> 

내 코드입니다 <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을 덤프하고 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 추적 수신기를 구성 :이 후

<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

엔터프라이즈 라이브러리로 코드를 복잡하게 만들 필요가 없습니다. XPathNavigator를 객체 인수로 전달하는 TraceSource의 TraceData() 메서드를 사용하면됩니다.

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