forked from microsoft/AzUrlShortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLogWrapper.cs
71 lines (57 loc) · 2.05 KB
/
NLogWrapper.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
using System;
using System.Web;
using Cloud5mins.domain;
using NLog;
public enum LoggerType
{
UrlRedirect
}
/// <summary>
/// A wrapper of NLog
/// </summary>
public class NLogWrapper
{
private Logger logger;
private string serviceName;
private ShortenerSettings shortenerSettings;
public NLogWrapper(LoggerType loggerType, ShortenerSettings settings)
{
initializeLogger(loggerType, settings);
}
public void Log(LogLevel logLevel, String message, string arg1 = null, string arg2 = null)
{
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("NLog.config");
var configuration = LogManager.Configuration;
configuration.FindTargetByName<NLog.Target.Datadog.DataDogTarget>("dataDog").ApiKey = shortenerSettings.dataDogKey;
LogManager.Configuration = configuration;
var tags = buildTags();
logger = logger.WithProperty("service", serviceName).WithProperty("env", shortenerSettings.env).WithProperty("version", shortenerSettings.version).WithProperty("ddtags", tags);
getCallerName();
logger.Log(logLevel, message, arg1, arg2);
}
private void initializeLogger(LoggerType loggerType, ShortenerSettings settings)
{
shortenerSettings = settings;
string loggerName = String.Empty;
switch (loggerType)
{
case LoggerType.UrlRedirect:
loggerName = "STORIS.UrlRedirect";
serviceName = "UrlRedirect";
break;
default:
break;
}
logger = LogManager.GetLogger(loggerName);
}
private string buildTags()
{
return String.Format("env:{0},service:{1},version:{2}", shortenerSettings.env, serviceName, shortenerSettings.version);
}
private void getCallerName()
{
var methodInfo = new System.Diagnostics.StackTrace().GetFrame(2).GetMethod();
var callerName = methodInfo.ReflectedType.FullName + '.' + methodInfo.Name;
logger = logger.WithProperty("method", callerName);
}
}