Created
December 9, 2016 15:51
-
-
Save neilgaietto/fd31d81a09ef07e274881cd7960ea23b to your computer and use it in GitHub Desktop.
Umbraco GrayLog Appender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web; | |
using AtlanticBT.Core.Logging; | |
using AtlanticBT.Core.Logging.Models; | |
using log4net.Appender; | |
using log4net.Core; | |
namespace AtlanticBT.Common.Umbraco.LogAppender | |
{ | |
public class GrayLogAppender : AppenderSkeleton | |
{ | |
protected override void Append(LoggingEvent loggingEvent) | |
{ | |
var logLevel = MapLogLevel(loggingEvent); | |
var message = loggingEvent.RenderedMessage; | |
if (Layout != null) | |
{ | |
message = RenderLoggingEvent(loggingEvent); | |
} | |
var httpContext = HttpContext.Current; | |
dynamic request = null; | |
if (httpContext != null) | |
{ | |
try | |
{ | |
var httpRequest = httpContext.Request; | |
request = new | |
{ | |
Url = httpRequest.Url, | |
UrlReferrer = httpRequest.UrlReferrer, | |
RequestType = httpRequest.RequestType, | |
FormData = httpRequest.Form | |
}; | |
} | |
catch (HttpException) | |
{ | |
//--This is thrown when the http request is not available | |
} | |
} | |
if (loggingEvent.ExceptionObject != null) | |
{ | |
AbtLoggingService.Log(message, "Umbraco Error", loggingEvent.ExceptionObject, logLevel, request); | |
} | |
else | |
{ | |
AbtLoggingService.Log(message, "Umbraco", level: logLevel, dataObject: request); | |
} | |
} | |
private static LogLevels MapLogLevel(LoggingEvent loggingEvent) | |
{ | |
if (loggingEvent.Level == Level.Alert) | |
{ | |
return LogLevels.Alert; | |
} | |
if (loggingEvent.Level == Level.Critical | |
|| loggingEvent.Level == Level.Fatal | |
|| loggingEvent.Level == Level.Severe) | |
{ | |
return LogLevels.Critical; | |
} | |
if (loggingEvent.Level == Level.Emergency) | |
{ | |
return LogLevels.Emergency; | |
} | |
if (loggingEvent.Level == Level.Warn) | |
{ | |
return LogLevels.Warn; | |
} | |
if (loggingEvent.Level == Level.Info) | |
{ | |
return LogLevels.Info; | |
} | |
if (loggingEvent.Level == Level.Debug) | |
{ | |
return LogLevels.Debug; | |
} | |
if (loggingEvent.Level == Level.Notice) | |
{ | |
return LogLevels.Notice; | |
} | |
return LogLevels.Error; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!-- | |
Example log4net.config setup to use the GrayLogAppender | |
Either use use config or follow these steps | |
1. Add this new appender to your config: <appender name="GrayLogAppender" ... | |
2. Reference the new appender in <root> by adding <appender-ref ref="GrayLogAppender" /> | |
3. Clear any unused appenders in <root> | |
--> | |
<log4net> | |
<root> | |
<priority value="Info"/> | |
<appender-ref ref="GrayLogAppender" /> | |
</root> | |
<appender name="GrayLogAppender" type="AtlanticBT.Common.Umbraco.LogAppender.GrayLogAppender,AtlanticBT.Common.Umbraco.LogAppender"> | |
<threshold value="INFO" /> | |
</appender> | |
</log4net> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment