-
Notifications
You must be signed in to change notification settings - Fork 2
/
LogExtensions.tt
131 lines (109 loc) · 6.14 KB
/
LogExtensions.tt
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
using System;
using JetBrains.Annotations;
using Vostok.Commons.Time;
using Vostok.Logging.Abstractions.Helpers;
namespace Vostok.Logging.Abstractions
{
[PublicAPI]
public static class LogExtensions
{
<#
foreach (var level in new [] { "Debug", "Info", "Warn", "Error", "Fatal" })
{
#>
#region <#=level #>
/// <summary>
/// Logs the given <paramref name="messageTemplate"/> on the <see cref="LogLevel.<#=level #>"/> level without any additional properties.
/// </summary>
public static void <#=level #>(this ILog log, [CanBeNull] string messageTemplate)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, messageTemplate));
}
/// <summary>
/// Logs the given <paramref name="exception"/> on the <see cref="LogLevel.<#=level #>"/> level without a message or any additional properties.
/// </summary>
public static void <#=level #>(this ILog log, [CanBeNull] Exception exception)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, null, exception));
}
/// <summary>
/// Logs the given <paramref name="messageTemplate"/> and <paramref name="exception"/> on the <see cref="LogLevel.<#=level #>"/> level without any additional properties.
/// </summary>
public static void <#=level #>(this ILog log, [CanBeNull] Exception exception, [CanBeNull] string messageTemplate)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, messageTemplate, exception));
}
/// <summary>
/// Logs the given <paramref name="messageTemplate"/> on the <see cref="LogLevel.<#=level #>"/> level with given <paramref name="properties" />. The <paramref name="messageTemplate"/> can contain placeholders for <paramref name="properties"/>, see <see cref="LogEvent.MessageTemplate"/> for details.
/// </summary>
public static void <#=level #><T>(this ILog log, [CanBeNull] string messageTemplate, [CanBeNull] T properties)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
if (!DeconstructionHelper.ShouldDeconstruct(messageTemplate, properties))
{
log.<#=level #>(messageTemplate, (object)properties);
return;
}
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, messageTemplate, LogPropertiesExtensions.GenerateInitialObjectProperties(properties, true), null));
}
/// <summary>
/// Logs the given <paramref name="messageTemplate"/> on the <see cref="LogLevel.<#=level #>"/> level with given <paramref name="parameters" />. The <paramref name="messageTemplate"/> can contain placeholders for <paramref name="parameters"/>, see <see cref="LogEvent.MessageTemplate"/> for details.
/// </summary>
public static void <#=level #>(this ILog log, [CanBeNull] string messageTemplate, [CanBeNull] params object[] parameters)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, messageTemplate, LogEventExtensions.GenerateInitialParameters(messageTemplate, parameters), null));
}
/// <summary>
/// Logs the given <paramref name="messageTemplate"/> and <paramref name="exception"/> on the <see cref="LogLevel.<#=level #>"/> level with given <paramref name="properties" />. The <paramref name="messageTemplate"/> can contain placeholders for <paramref name="properties"/>, see <see cref="LogEvent.MessageTemplate"/> for details.
/// </summary>
public static void <#=level #><T>(this ILog log, [CanBeNull] Exception exception, [CanBeNull] string messageTemplate, [CanBeNull] T properties)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
if (!DeconstructionHelper.ShouldDeconstruct(messageTemplate, properties))
{
log.<#=level #>(exception, messageTemplate, (object)properties);
return;
}
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, messageTemplate, LogPropertiesExtensions.GenerateInitialObjectProperties(properties, true), exception));
}
/// <summary>
/// Logs the given <paramref name="messageTemplate"/> and <paramref name="exception"/> on the <see cref="LogLevel.<#=level #>"/> level with given <paramref name="parameters" />. The <paramref name="messageTemplate"/> can contain placeholders for <paramref name="parameters"/>, see <see cref="LogEvent.MessageTemplate"/> for details.
/// </summary>
public static void <#=level #>(this ILog log, [CanBeNull] Exception exception, [CanBeNull] string messageTemplate, [CanBeNull] params object[] parameters)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
log.Log(new LogEvent(LogLevel.<#=level #>, PreciseDateTime.Now, messageTemplate, LogEventExtensions.GenerateInitialParameters(messageTemplate, parameters), exception));
}
[Obsolete("Use the <#=level #>(ILog, Exception, string) overload instead.")]
public static void <#=level #>(this ILog log, [CanBeNull] string messageTemplate, [CanBeNull] Exception exception)
{
if (!log.IsEnabledFor(LogLevel.<#=level #>))
return;
log.<#=level #>(exception, messageTemplate);
}
/// <summary>
/// Returns true if given <paramref name="log"/> is enabled to log events of <see cref="LogLevel.<#=level #>"/> level, or false otherwise.
/// </summary>
public static bool IsEnabledFor<#=level #>(this ILog log)
{
return log.IsEnabledFor(LogLevel.<#=level #>);
}
#endregion
<#
}
#>
}
}