簡體   English   中英

自定義msmq跟蹤偵聽器

[英]Custom msmq trace listener

Microsoft Enterprise Library使我們能夠使用CustomTraceListener作為基類來創建自定義跟蹤偵聽器。

但是我的問題是如何創建自定義消息隊列跟蹤偵聽器,並能夠在企業庫配置控制台中使用它?

請幫忙,因為我已經嘗試過搜索它,但找不到任何解決方案。

我嘗試使用以下過程創建它

public class myCustomListener : CustomTraceListener
{
    //Trace Listener Code Here
}

但是我需要創建一個自定義msmq跟蹤偵聽器

您可以編寫此類。

[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class MyCustomMsmqTraceListener : CustomTraceListener
{
    public override void Write(object o)
    {
        XmlSerializer serializer = new XmlSerializer(o.GetType());
        MemoryStream ms = new MemoryStream();
        serializer.Serialize(ms, o);
        string serializedMessage = new StreamReader(ms).ReadToEnd();

        this.Write(serializedMessage);
    }

    public override void Write(string message)
    {
        SendMessageToQueue(message);
    }

    public override void WriteLine(string message)
    {
        SendMessageToQueue(message);
    }

    private void SendMessageToQueue(string message)
    {
        string queueName = @".\Private$\test";

        if (!MessageQueue.Exists(queueName))
            MessageQueue.Create(queueName);

        MessageQueue mq = new MessageQueue(queueName, QueueAccessMode.Send);
        mq.Label = DateTime.Now.ToString();
        mq.Send(message);
        mq.Close();
    }
}

並在以下部分編寫app.config。

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                type="EnterpriseLibrary.Sample1.MyCustomMsmqTraceListener, EnterpriseLibrary.Sample1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                 name="MyCustomMsmqTraceListener"
                formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
          name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="MyCustomMsmqTraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="MyCustomMsmqTraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

為了進行測試,您可以創建控制台應用程序。

Exception ex = new Exception("Sample exception.."); // Or your custom class.
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(ex);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM