문제

I'm using TraceSource to log information to a XmlWriterTraceListener. The message I'm logging is a XML, however, when I view the message in Service Trace Viewer, it's not displayed as a XML, it's displayed as a string. Is there a way to do this? Here is my 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>

Below is my code:

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

And below is how it's showing in Service Trace Viewer

enter image description here

Is there anyway so that what's under the <ApplicationData> tag is formatted as a XML?

EDIT

I opened the svcfile, and I saw that the string is not encoded properly. Why isn't it?

<ApplicationData>&lt;log Método=&quot;RunSql&quot;&gt;drop procedure dbo.spfwug_in_controle_versao&lt;/log&gt;</ApplicationData>
도움이 되었습니까?

해결책 2

I was able to do this dumping the TraceSource, and using the Enterprise Library 5.0. It was a XmlLogEntry that solved my problem. Below is the code:

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

After that, I configure a XML Trace Listener in the 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>

After this, the XML that I send is correctly formatted as a XML in the svclog format.

다른 팁

No need to clutter your code with the enterprise library; just use the TraceData() method of the TraceSource passing an XPathNavigator as the object argument:

TextReader reader = new StringReader(message);
var xml = new XPathDocument(reader).CreateNavigator();
this.traceSource.TraceData(TraceEventType.Information, -2, xml);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top