jessyhoule

C# / Custom Loggin

Aug 20th, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. /* web/app.config
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <configuration>
  4.   <appSettings>
  5.     <!-- log4net settings-->
  6.     <add key="log4net.Config" value="Log4net_normal.xml" />
  7.     <add key="log4net.Config.Watch" value="True" />
  8.     <add key="log4net.Internal.Debug" value="True" />
  9.    
  10.     <!-- notification settings -->
  11.     <add key="NotificationFrom" value="[email protected]" />
  12.     <add key="NotificationContentType" value="HTML" />
  13.     <add key="NotificationSubject" value="DEV Ashley Furniture:WMS Transfer adapter failure notification" />
  14.     <add key="SendNotifications" value="True" />
  15.   </appSettings>
  16.   <startup>
  17.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  18.   </startup>
  19. </configuration>
  20. */
  21.  
  22. /* Log4net_normal.xml
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <log4net debug="true">
  25.   <appender name="NotificationAppender" type="Ashley.Distribution.Wms.Common.NotificationAppender, Ashley.Distribution.Wms.Common">
  26.     <bufferSize value="0"/>
  27.     <Environment value="AFI"/>
  28.     <threshold value="DEBUG"/>
  29.     <layout type="log4net.Layout.PatternLayout">
  30.       <conversionPattern value="%date %thread %-5level %property{SessionUsername} %logger{1} %method - %message%newline"/>
  31.     </layout>
  32.   </appender>
  33.   <root>
  34.     <priority value="ALL"/>
  35.     <level value="DEBUG"/>
  36.     <appender-ref ref="NotificationAppender" />
  37.   </root>
  38.   <logger additivity="false" name="Ashley.Distribution.Wms.Common.NotificationAppender">
  39.     <level value="DEBUG"/>
  40.   </logger>
  41. </log4net>
  42. */
  43.  
  44. using System.Configuration;
  45. using System.Diagnostics;
  46. using System.Linq;
  47. using Ashley.Distribution.Wms.Bootstrap;
  48. using Ashley.Distribution.Wms.Contracts.IntegrationServices;
  49. using Ashley.Integration.SDK;
  50. using log4net.Appender;
  51. using log4net.Core;
  52.  
  53. namespace Ashley.Distribution.Wms.Common
  54. {
  55.     public class NotificationAppender : BufferingAppenderSkeleton
  56.     {
  57.         protected override void SendBuffer(LoggingEvent[] loggingEvents)
  58.         {
  59.             var sendNotifications = ConfigurationManager.AppSettings.AllKeys.Contains("SendNotifications") && ConfigurationManager.AppSettings["SendNotifications"] == "True";
  60.             var integrationContext = IoCContainer.Resolve<IIntegrationContext>();
  61.  
  62.             if (sendNotifications && integrationContext != null)
  63.             {
  64.                 foreach (var loggingEvent in loggingEvents)
  65.                 {
  66.                     if (loggingEvent.Level.Value >= Level.Error.Value)
  67.                     {
  68.                         var toEmailAddress = ConfigurationManager.AppSettings["NotificationToList"];
  69.                         var fromEmailAddress = ConfigurationManager.AppSettings["NotificationFrom"];
  70.                         var contentType = ConfigurationManager.AppSettings["NotificationContentType"];
  71.                         var subjectTemplate = ConfigurationManager.AppSettings["NotificationSubject"];
  72.                         var message = loggingEvent.MessageObject.ToString();
  73.  
  74.                         integrationContext.PublishEnterpriseNotificationEvent(toEmailAddress, fromEmailAddress, contentType, subjectTemplate, message);
  75.                         integrationContext.PublishEnterpriseTraceEvent(eTraceInfoType.ERROR, message);
  76.                         integrationContext.PublishLocalApplicationEventLogEvent(message, EventLogEntryType.Error);
  77.                     }
  78.  
  79.                     if (loggingEvent.Level == Level.Warn)
  80.                     {
  81.                         var message = loggingEvent.MessageObject.ToString();
  82.  
  83.                         integrationContext.PublishEnterpriseTraceEvent(eTraceInfoType.WARNING, message);
  84.                         integrationContext.PublishLocalApplicationEventLogEvent(message, EventLogEntryType.Warning);
  85.                     }
  86.  
  87.                     if (loggingEvent.Level == Level.Debug || loggingEvent.Level == Level.Info)
  88.                     {
  89.                         var message = loggingEvent.MessageObject.ToString();
  90.  
  91.                         integrationContext.PublishEnterpriseTraceEvent(eTraceInfoType.INFO, message);
  92.                         integrationContext.PublishLocalApplicationEventLogEvent(message, EventLogEntryType.Information);
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment