我正在使用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();
}
}
}
及以下它是如何顯示服務跟蹤查看
有反正讓<ApplicationData>
標記下的內容被格式化爲XML?
編輯
我打開svcfile,我看到該字符串不正確編碼。爲什麼不是?
<ApplicationData><log Método="RunSql">drop procedure dbo.spfwug_in_controle_versao</log></ApplicationData>