自定义msmq跟踪侦听器

时间:2013-12-22 07:39:53

标签: c# msmq enterprise-library

Microsoft Enterprise Library使我们能够使用CustomTraceListener作为基类创建自定义跟踪侦听器。

但我的问题是如何创建自定义消息队列跟踪侦听器并能够在企业库配置控制台中使用它?

请帮忙,因为我已经尝试谷歌但却找不到任何解决方案。

我尝试使用以下程序创建它

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

但我需要创建一个自定义的msmq跟踪侦听器

1 个答案:

答案 0 :(得分:1)

你可以写这个班..

[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);