Skip to content

Commit ddd57f0

Browse files
committed
Merge pull request #11 from danbarua/master
Tweaks to NLog logger
2 parents ee881ef + d99b0d1 commit ddd57f0

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

src/ServiceStack.Logging.NLog/NLogFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ServiceStack.Logging.NLogger
66
/// <summary>
77
/// ILogFactory that creates an NLog ILog logger
88
/// </summary>
9-
public class NLogFactory : ServiceStack.Logging.ILogFactory
9+
public class NLogFactory : ServiceStack.Logging.ILogFactory
1010
{
1111
/// <summary>
1212
/// Initializes a new instance of the <see cref="NLogFactory"/> class.

src/ServiceStack.Logging.NLog/NLogLogger.cs

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
2+
using NLog;
23
using ServiceStack.Logging;
34

45
namespace ServiceStack.Logging.NLogger
56
{
67
/// <summary>
78
/// Wrapper over the NLog 2.0 beta and above logger
89
/// </summary>
9-
public class NLogLogger : ServiceStack.Logging.ILog
10+
public class NLogLogger : ServiceStack.Logging.ILog
1011
{
1112
private readonly NLog.Logger log;
1213

@@ -21,18 +22,29 @@ public NLogLogger(string typeName)
2122
/// <param name="type">The type.</param>
2223
public NLogLogger(Type type)
2324
{
24-
log = NLog.LogManager.GetLogger(type.Name);
25+
log = NLog.LogManager.GetLogger(UseFullTypeNames ? type.FullName : type.Name);
2526
}
2627

27-
public bool IsDebugEnabled { get { return log.IsDebugEnabled; } }
28-
29-
/// <summary>
28+
public static bool UseFullTypeNames { get; set; }
29+
30+
public bool IsDebugEnabled { get { return log.IsDebugEnabled; } }
31+
32+
public bool IsInfoEnabled { get { return log.IsInfoEnabled; } }
33+
34+
public bool IsWarnEnabled { get { return log.IsWarnEnabled; } }
35+
36+
public bool IsErrorEnabled { get { return log.IsErrorEnabled; } }
37+
38+
public bool IsFatalEnabled { get { return log.IsFatalEnabled; } }
39+
40+
/// <summary>
3041
/// Logs a Debug message.
3142
/// </summary>
3243
/// <param name="message">The message.</param>
3344
public void Debug(object message)
3445
{
35-
log.Debug(message);
46+
if (IsDebugEnabled)
47+
Write(LogLevel.Debug, message.ToString());
3648
}
3749

3850
/// <summary>
@@ -42,7 +54,8 @@ public void Debug(object message)
4254
/// <param name="exception">The exception.</param>
4355
public void Debug(object message, Exception exception)
4456
{
45-
log.DebugException(message.ToString(), exception);
57+
if(IsDebugEnabled)
58+
Write(LogLevel.Debug,exception,message.ToString());
4659
}
4760

4861
/// <summary>
@@ -52,7 +65,8 @@ public void Debug(object message, Exception exception)
5265
/// <param name="args">The args.</param>
5366
public void DebugFormat(string format, params object[] args)
5467
{
55-
log.Debug(format, args);
68+
if (IsDebugEnabled)
69+
Write(LogLevel.Debug, format, args);
5670
}
5771

5872
/// <summary>
@@ -61,7 +75,8 @@ public void DebugFormat(string format, params object[] args)
6175
/// <param name="message">The message.</param>
6276
public void Error(object message)
6377
{
64-
log.Error(message);
78+
if (IsErrorEnabled)
79+
Write(LogLevel.Error,message.ToString());
6580
}
6681

6782
/// <summary>
@@ -71,7 +86,8 @@ public void Error(object message)
7186
/// <param name="exception">The exception.</param>
7287
public void Error(object message, Exception exception)
7388
{
74-
log.ErrorException(message.ToString(), exception);
89+
if (IsErrorEnabled)
90+
Write(LogLevel.Error, exception, message.ToString());
7591
}
7692

7793
/// <summary>
@@ -81,7 +97,8 @@ public void Error(object message, Exception exception)
8197
/// <param name="args">The args.</param>
8298
public void ErrorFormat(string format, params object[] args)
8399
{
84-
log.Error(format, args);
100+
if (IsErrorEnabled)
101+
Write(LogLevel.Error,format,args);
85102
}
86103

87104
/// <summary>
@@ -90,7 +107,8 @@ public void ErrorFormat(string format, params object[] args)
90107
/// <param name="message">The message.</param>
91108
public void Fatal(object message)
92109
{
93-
log.Fatal(message);
110+
if (IsFatalEnabled)
111+
Write(LogLevel.Fatal,message.ToString());
94112
}
95113

96114
/// <summary>
@@ -100,7 +118,8 @@ public void Fatal(object message)
100118
/// <param name="exception">The exception.</param>
101119
public void Fatal(object message, Exception exception)
102120
{
103-
log.FatalException(message.ToString(), exception);
121+
if (IsFatalEnabled)
122+
Write(LogLevel.Fatal, exception, message.ToString());
104123
}
105124

106125
/// <summary>
@@ -110,7 +129,8 @@ public void Fatal(object message, Exception exception)
110129
/// <param name="args">The args.</param>
111130
public void FatalFormat(string format, params object[] args)
112131
{
113-
log.Fatal(format, args);
132+
if (IsFatalEnabled)
133+
Write(LogLevel.Fatal, format, args);
114134
}
115135

116136
/// <summary>
@@ -119,7 +139,8 @@ public void FatalFormat(string format, params object[] args)
119139
/// <param name="message">The message.</param>
120140
public void Info(object message)
121141
{
122-
log.Info(message);
142+
if (IsInfoEnabled)
143+
Write(LogLevel.Info,message.ToString());
123144
}
124145

125146
/// <summary>
@@ -129,7 +150,8 @@ public void Info(object message)
129150
/// <param name="exception">The exception.</param>
130151
public void Info(object message, Exception exception)
131152
{
132-
log.InfoException(message.ToString(), exception);
153+
if (IsInfoEnabled)
154+
Write(LogLevel.Info,exception,message.ToString());
133155
}
134156

135157
/// <summary>
@@ -139,7 +161,8 @@ public void Info(object message, Exception exception)
139161
/// <param name="args">The args.</param>
140162
public void InfoFormat(string format, params object[] args)
141163
{
142-
log.Info(format, args);
164+
if (IsInfoEnabled)
165+
Write(LogLevel.Info, format, args);
143166
}
144167

145168
/// <summary>
@@ -148,7 +171,8 @@ public void InfoFormat(string format, params object[] args)
148171
/// <param name="message">The message.</param>
149172
public void Warn(object message)
150173
{
151-
log.Warn(message);
174+
if (IsWarnEnabled)
175+
Write(LogLevel.Warn,message.ToString());
152176
}
153177

154178
/// <summary>
@@ -158,7 +182,8 @@ public void Warn(object message)
158182
/// <param name="exception">The exception.</param>
159183
public void Warn(object message, Exception exception)
160184
{
161-
log.WarnException(message.ToString(), exception);
185+
if (IsWarnEnabled)
186+
Write(LogLevel.Warn,exception,message.ToString());
162187
}
163188

164189
/// <summary>
@@ -168,7 +193,21 @@ public void Warn(object message, Exception exception)
168193
/// <param name="args">The args.</param>
169194
public void WarnFormat(string format, params object[] args)
170195
{
171-
log.Warn(format, args);
196+
if (IsWarnEnabled)
197+
Write(LogLevel.Warn, format, args);
198+
}
199+
200+
private void Write(LogLevel level, string format, params object[] args)
201+
{
202+
//preserve call site info - see here: http://stackoverflow.com/questions/3947136/problem-matching-specific-nlog-logger-name
203+
var logEventInfo = new LogEventInfo(level, log.Name, null, format, args);
204+
log.Log(typeof(NLogLogger), logEventInfo);
205+
}
206+
207+
private void Write(LogLevel level, Exception exception, string format, params object[] args)
208+
{
209+
var exceptionEventInfo = new LogEventInfo(level, log.Name, null, format, args, exception);
210+
log.Log(typeof(NLogLogger), exceptionEventInfo);
172211
}
173212
}
174213
}

0 commit comments

Comments
 (0)