2013-12-22 110 views
0

Microsoft企业库使我们能够使用CustomTraceListener作为基类来创建自定义跟踪侦听器。自定义msmq跟踪侦听器

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

请帮忙,因为我已经试图谷歌它,但无法找到任何解决方案。

我尝试使用下面的过程

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

创建它,但我需要创建一个自定义MSMQ跟踪侦听

回答

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

而写的应用程序.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);