-
Notifications
You must be signed in to change notification settings - Fork 5
/
LoggerFactoryBuilder.cs
84 lines (71 loc) · 3.46 KB
/
LoggerFactoryBuilder.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
using System.Fabric;
using System.Globalization;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Context;
using ServiceFabric.Logging.ApplicationInsights;
using ServiceFabric.Logging.PropertyMap;
using ServiceFabric.Remoting.CustomHeaders;
namespace ServiceFabric.Logging
{
/// <summary>
/// Implementation of <see cref="ILoggerFactoryBuilder"/>
/// </summary>
public class LoggerFactoryBuilder : ILoggerFactoryBuilder
{
private readonly ServiceContext _context;
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="context">The <see cref="ServiceContext"/> of the service or actor to monitor</param>
public LoggerFactoryBuilder(ServiceContext context)
{
_context = context;
}
/// <summary>
/// Creates an instance of <see cref="ILoggerFactory"/> that provides logging to application insights using SeriLog
/// </summary>
/// <param name="aiKey">The Application Insights key used for logging</param>
/// <returns>An instance of <see cref="LoggerFactory"/></returns>
public ILoggerFactory CreateLoggerFactory(string aiKey)
{
var configuration = new TelemetryConfiguration
{
InstrumentationKey = aiKey
};
configuration.TelemetryInitializers.Add(new OperationContextTelemetryInitializer(() =>
RemotingContext.GetData(HeaderIdentifiers.TraceId)?.ToString()));
new LiveStreamProvider(configuration).Enable();
var loggerFactory = new LoggerFactory();
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo
.ApplicationInsights(
configuration,
(logEvent, formatter) => new TelemetryBuilder(_context, logEvent).LogEventToTelemetryConverter())
.CreateLogger();
InitContextProperties();
loggerFactory.AddSerilog(logger, true);
return loggerFactory;
}
private void InitContextProperties()
{
LogContext.PushProperty(ServiceContextProperties.ServiceTypeName, _context.ServiceTypeName);
LogContext.PushProperty(ServiceContextProperties.ServiceName, _context.ServiceName);
LogContext.PushProperty(ServiceContextProperties.PartitionId, _context.PartitionId);
LogContext.PushProperty(ServiceContextProperties.NodeName, _context.NodeContext.NodeName);
LogContext.PushProperty(ServiceContextProperties.ApplicationName, _context.CodePackageActivationContext.ApplicationName);
LogContext.PushProperty(ServiceContextProperties.ApplicationTypeName, _context.CodePackageActivationContext.ApplicationTypeName);
LogContext.PushProperty(ServiceContextProperties.ServicePackageVersion, _context.CodePackageActivationContext.CodePackageVersion);
if (_context is StatelessServiceContext)
{
LogContext.PushProperty(ServiceContextProperties.InstanceId, _context.ReplicaOrInstanceId.ToString(CultureInfo.InvariantCulture));
}
else if (_context is StatefulServiceContext)
{
LogContext.PushProperty(ServiceContextProperties.ReplicaId, _context.ReplicaOrInstanceId.ToString(CultureInfo.InvariantCulture));
}
}
}
}