简体   繁体   English

自定义msmq跟踪侦听器

[英]Custom msmq trace listener

The Microsoft Enterprise Library gives us the ability to create custom trace listeners using the CustomTraceListener as a base class. Microsoft Enterprise Library使我们能够使用CustomTraceListener作为基类来创建自定义跟踪侦听器。

But my question is how can i create a custom message queue trace listener and be able to use it in the enterprise library confiugration console? 但是我的问题是如何创建自定义消息队列跟踪侦听器,并能够在企业库配置控制台中使用它?

Please help as I have already tried to google it but could not find any solutions. 请帮忙,因为我已经尝试过搜索它,但找不到任何解决方案。

I tried to create it using following procedure 我尝试使用以下过程创建它

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

But I need to create a custom msmq trace listener 但是我需要创建一个自定义msmq跟踪侦听器

You can write this class.. 您可以编写此类。

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

And write app.config below section.. 并在以下部分编写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>

For test, you can create console app. 为了进行测试,您可以创建控制台应用程序。

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