jessyhoule icon

C# / Custom Loggin

jessyhoule | PRO | 08/20/15 09:00:44 PM UTC | 0 ⭐ | 450 👁️ | Never ⏰ | []
C# |

4.5 KB

|

None

|

0 👍

/

0 👎

/* web/app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <!-- log4net settings-->
    <add key="log4net.Config" value="Log4net_normal.xml" />
    <add key="log4net.Config.Watch" value="True" />
    <add key="log4net.Internal.Debug" value="True" />
    
    <!-- notification settings -->
    <add key="NotificationToList" value="[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];" />
    <add key="NotificationFrom" value="[email protected]" />
    <add key="NotificationContentType" value="HTML" />
    <add key="NotificationSubject" value="DEV Ashley Furniture:WMS Transfer adapter failure notification" />
    <add key="SendNotifications" value="True" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
*/
 
/* Log4net_normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<log4net debug="true">
  <appender name="NotificationAppender" type="Ashley.Distribution.Wms.Common.NotificationAppender, Ashley.Distribution.Wms.Common">
    <bufferSize value="0"/>
    <Environment value="AFI"/>
    <threshold value="DEBUG"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %thread %-5level %property{SessionUsername} %logger{1} %method - %message%newline"/>
    </layout>
  </appender>
  <root>
    <priority value="ALL"/>
    <level value="DEBUG"/>
    <appender-ref ref="NotificationAppender" />
  </root>
  <logger additivity="false" name="Ashley.Distribution.Wms.Common.NotificationAppender">
    <level value="DEBUG"/>
  </logger>
</log4net>
*/
 
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using Ashley.Distribution.Wms.Bootstrap;
using Ashley.Distribution.Wms.Contracts.IntegrationServices;
using Ashley.Integration.SDK;
using log4net.Appender;
using log4net.Core;
 
namespace Ashley.Distribution.Wms.Common
{
    public class NotificationAppender : BufferingAppenderSkeleton
    {
        protected override void SendBuffer(LoggingEvent[] loggingEvents)
        {
            var sendNotifications = ConfigurationManager.AppSettings.AllKeys.Contains("SendNotifications") && ConfigurationManager.AppSettings["SendNotifications"] == "True";
            var integrationContext = IoCContainer.Resolve<IIntegrationContext>();
 
            if (sendNotifications && integrationContext != null)
            {
                foreach (var loggingEvent in loggingEvents)
                {
                    if (loggingEvent.Level.Value >= Level.Error.Value)
                    {
                        var toEmailAddress = ConfigurationManager.AppSettings["NotificationToList"];
                        var fromEmailAddress = ConfigurationManager.AppSettings["NotificationFrom"];
                        var contentType = ConfigurationManager.AppSettings["NotificationContentType"];
                        var subjectTemplate = ConfigurationManager.AppSettings["NotificationSubject"];
                        var message = loggingEvent.MessageObject.ToString();
 
                        integrationContext.PublishEnterpriseNotificationEvent(toEmailAddress, fromEmailAddress, contentType, subjectTemplate, message);
                        integrationContext.PublishEnterpriseTraceEvent(eTraceInfoType.ERROR, message);
                        integrationContext.PublishLocalApplicationEventLogEvent(message, EventLogEntryType.Error);
                    }
 
                    if (loggingEvent.Level == Level.Warn)
                    {
                        var message = loggingEvent.MessageObject.ToString();
 
                        integrationContext.PublishEnterpriseTraceEvent(eTraceInfoType.WARNING, message);
                        integrationContext.PublishLocalApplicationEventLogEvent(message, EventLogEntryType.Warning);
                    }
 
                    if (loggingEvent.Level == Level.Debug || loggingEvent.Level == Level.Info)
                    {
                        var message = loggingEvent.MessageObject.ToString();
 
                        integrationContext.PublishEnterpriseTraceEvent(eTraceInfoType.INFO, message);
                        integrationContext.PublishLocalApplicationEventLogEvent(message, EventLogEntryType.Information);
                    }
                }
            }
        }
    }
}

Comments