-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApplicationInsightsAppender.cs
105 lines (94 loc) · 4.86 KB
/
ApplicationInsightsAppender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// <copyright file="ApplicationInsightsAppender.cs" company="Engage Software">
// Engage: Application Insights
// Copyright (c) 2004-2016
// by Engage Software ( http://www.engagesoftware.com )
// </copyright>
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace Engage.Dnn.ApplicationInsights
{
using System;
using System.Collections.Generic;
using System.Globalization;
using log4net.Appender;
using log4net.Core;
using Microsoft.ApplicationInsights;
/// <summary>A Log4Net appender for Application Insights</summary>
/// <remarks>We can't use the official appender because DNN doesn't use the official Log4Net assembly</remarks>
public class ApplicationInsightsAppender : AppenderSkeleton
{
/// <summary>The telemetry client</summary>
private readonly TelemetryClient telemetryClient = new TelemetryClient();
/// <summary>Appends the logging event.</summary>
/// <param name="loggingEvent">The logging event.</param>
/// <exception cref="LogException">When there's an exception</exception>
protected override void Append(LoggingEvent loggingEvent)
{
try
{
if (loggingEvent.ExceptionObject == null)
{
this.telemetryClient.TrackTrace(loggingEvent.RenderedMessage ?? string.Empty, GetProperties(loggingEvent));
}
else
{
this.telemetryClient.TrackException(loggingEvent.ExceptionObject, GetProperties(loggingEvent), new Dictionary<string, double>(0));
}
}
catch (ArgumentNullException ex)
{
throw new LogException(ex.Message, ex);
}
}
/// <summary>Gets the properties of the event.</summary>
/// <param name="loggingEvent">The logging event.</param>
/// <returns>A <see cref="IDictionary{T,U}" /> instance.</returns>
private static IDictionary<string, string> GetProperties(LoggingEvent loggingEvent)
{
var properties = new Dictionary<string, string> { { "SourceType", "Log4Net" }, };
AddLogProperty("LoggerName", loggingEvent.LoggerName, properties);
AddLogProperty("LoggingLevel", loggingEvent.Level != null ? loggingEvent.Level.Name : null, properties);
AddLogProperty("ThreadName", loggingEvent.ThreadName, properties);
AddLogProperty("TimeStamp", loggingEvent.TimeStamp, properties);
AddLogProperty("UserName", loggingEvent.UserName, properties);
AddLogProperty("Domain", loggingEvent.Domain, properties);
AddLogProperty("Identity", loggingEvent.Identity, properties);
var locationInformation = loggingEvent.LocationInformation;
if (locationInformation != null)
{
AddLogProperty("ClassName", locationInformation.ClassName, properties);
AddLogProperty("FileName", locationInformation.FileName, properties);
AddLogProperty("MethodName", locationInformation.MethodName, properties);
AddLogProperty("LineNumber", locationInformation.LineNumber, properties);
}
return properties;
}
/// <summary>Adds the property to the log properties, if <paramref name="value"/> is not <c>null</c>.</summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <param name="properties">The properties collection.</param>
private static void AddLogProperty(string key, string value, IDictionary<string, string> properties)
{
if (value == null)
{
return;
}
properties.Add(key, value);
}
/// <summary>Adds the property to the log properties, if <paramref name="value"/> is not <c>null</c>.</summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <param name="properties">The properties collection.</param>
private static void AddLogProperty(string key, IConvertible value, IDictionary<string, string> properties)
{
if (value == null)
{
return;
}
AddLogProperty(key, value.ToString(CultureInfo.InvariantCulture), properties);
}
}
}