Skip to content

Commit a45cb41

Browse files
committed
Logging: changed signatures of all logging methods
1 parent 753927b commit a45cb41

47 files changed

Lines changed: 210 additions & 200 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Libraries/SmartStore.Core/IO/VirtualPath/DefaultVirtualPathProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ public virtual string ToAppRelative(string virtualPath)
102102
// Result : /Blah/Blah2 <= that is not an app relative path!
103103
if (!result.StartsWith("~/"))
104104
{
105-
_logger.Information("Path '{0}' cannot be made app relative: Path returned ('{1}') is not app relative.".FormatCurrent(virtualPath, result));
105+
_logger.Info("Path '{0}' cannot be made app relative: Path returned ('{1}') is not app relative.".FormatCurrent(virtualPath, result));
106106
return null;
107107
}
108108
return result;
109109
}
110110
catch (Exception e)
111111
{
112112
// The initial path might have been invalid (e.g. path indicates a path outside the application root)
113-
_logger.Information("Path '{0}' cannot be made app relative".FormatCurrent(virtualPath), e);
113+
_logger.Info(e, "Path '{0}' cannot be made app relative".FormatCurrent(virtualPath));
114114
return null;
115115
}
116116
}
@@ -143,7 +143,7 @@ public bool IsMalformedVirtualPath(string virtualPath)
143143
{
144144
if (depth == 0)
145145
{
146-
_logger.Information("Path '{0}' cannot be made app relative: Too many '..'".FormatCurrent(virtualPath));
146+
_logger.Info("Path '{0}' cannot be made app relative: Too many '..'".FormatCurrent(virtualPath));
147147
return true;
148148
}
149149
depth--;

src/Libraries/SmartStore.Core/Logging/ILogger.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
using System;
2-
using SmartStore.Core.Domain.Customers;
3-
using SmartStore.Core.Domain.Logging;
42

53
namespace SmartStore.Core.Logging
64
{
7-
/// <summary>
8-
/// Logger interface
9-
/// </summary>
10-
public partial interface ILogger
5+
/// <summary>
6+
/// Interface that all loggers implement
7+
/// </summary>
8+
public partial interface ILogger
119
{
12-
/// <summary>
13-
/// Determines whether a log level is enabled
14-
/// </summary>
15-
/// <param name="level">Log level</param>
16-
/// <returns>Result</returns>
17-
bool IsEnabledFor(LogLevel level);
10+
/// <summary>
11+
/// Checks if this logger is enabled for a given <see cref="LogLevel"/> passed as parameter.
12+
/// </summary>
13+
/// <param name="level">true if this logger is enabled for level, otherwise false</param>
14+
/// <returns>Result</returns>
15+
bool IsEnabledFor(LogLevel level);
1816

19-
/// <summary>
20-
/// Inserts a log item
21-
/// </summary>
22-
/// <param name="logLevel">Log level</param>
23-
/// <param name="shortMessage">The short message</param>
24-
/// <param name="fullMessage">The full message</param>
25-
/// <returns>Always return <c>null</c></returns>
26-
void Log(LogLevel logLevel, string shortMessage, string fullMessage = "");
17+
/// <summary>
18+
/// Generates a logging event for the specified level using the message and exception
19+
/// </summary>
20+
/// <param name="logLevel">The level of the message to be logged</param>
21+
/// <param name="exception">The exception to log, including its stack trace. Pass null to not log an exception</param>
22+
/// <param name="message">The message object to log</param>
23+
/// <param name="args">An Object array containing zero or more objects to format. Can be null.</param>
24+
void Log(LogLevel level, Exception exception, string message, object[] args);
2725

2826
/// <summary>
2927
/// Commits log entries to the data store
Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,72 @@
11
using System;
2-
using SmartStore.Core.Domain.Customers;
3-
using SmartStore.Core.Domain.Logging;
42

53
namespace SmartStore.Core.Logging
64
{
75
public static class LoggingExtensions
8-
{
9-
public static void Debug(this ILogger logger, string message, Exception exception = null)
10-
{
11-
FilteredLog(logger, LogLevel.Debug, message, null, exception);
12-
}
6+
{
7+
public static bool IsDebugEnabled(this ILogger l) => l.IsEnabledFor(LogLevel.Debug);
8+
public static bool IsInfoEnabled(this ILogger l) => l.IsEnabledFor(LogLevel.Information);
9+
public static bool IsWarnEnabled(this ILogger l) => l.IsEnabledFor(LogLevel.Warning);
10+
public static bool IsErrorEnabled(this ILogger l) => l.IsEnabledFor(LogLevel.Error);
11+
public static bool IsFatalEnabled(this ILogger l) => l.IsEnabledFor(LogLevel.Fatal);
1312

14-
public static void Information(this ILogger logger, string message, Exception exception = null)
15-
{
16-
FilteredLog(logger, LogLevel.Information, message, null, exception);
17-
}
1813

19-
public static void Warning(this ILogger logger, string message, Exception exception = null)
20-
{
21-
FilteredLog(logger, LogLevel.Warning, message, null, exception);
22-
}
2314

24-
public static void Error(this ILogger logger, string message, Exception exception = null)
25-
{
26-
FilteredLog(logger, LogLevel.Error, message, null, exception);
27-
}
15+
public static void Debug(this ILogger l, string msg) => FilteredLog(l, LogLevel.Debug, null, msg, null);
16+
public static void Debug(this ILogger l, Func<string> msgFactory) => FilteredLog(l, LogLevel.Debug, null, msgFactory);
17+
public static void Debug(this ILogger l, Exception ex, string msg) => FilteredLog(l, LogLevel.Debug, ex, msg, null);
18+
public static void DebugFormat(this ILogger l, string msg, params object[] args) => FilteredLog(l, LogLevel.Debug, null, msg, args);
19+
public static void DebugFormat(this ILogger l, Exception ex, string msg, params object[] args) => FilteredLog(l, LogLevel.Debug, ex, msg, args);
2820

29-
public static void Error(this ILogger logger, string message, string fullMessage, Exception exception = null)
30-
{
31-
FilteredLog(logger, LogLevel.Error, message, fullMessage, exception);
32-
}
21+
public static void Info(this ILogger l, string msg) => FilteredLog(l, LogLevel.Information, null, msg, null);
22+
public static void Info(this ILogger l, Func<string> msgFactory) => FilteredLog(l, LogLevel.Information, null, msgFactory);
23+
public static void Info(this ILogger l, Exception ex, string msg) => FilteredLog(l, LogLevel.Information, ex, msg, null);
24+
public static void InfoFormat(this ILogger l, string msg, params object[] args) => FilteredLog(l, LogLevel.Information, null, msg, args);
25+
public static void InfoFormat(this ILogger l, Exception ex, string msg, params object[] args) => FilteredLog(l, LogLevel.Information, ex, msg, args);
3326

34-
public static void Fatal(this ILogger logger, string message, Exception exception = null)
35-
{
36-
FilteredLog(logger, LogLevel.Fatal, message, null, exception);
37-
}
27+
public static void Warn(this ILogger l, string msg) => FilteredLog(l, LogLevel.Warning, null, msg, null);
28+
public static void Warn(this ILogger l, Func<string> msgFactory) => FilteredLog(l, LogLevel.Warning, null, msgFactory);
29+
public static void Warn(this ILogger l, Exception ex, string msg) => FilteredLog(l, LogLevel.Warning, ex, msg, null);
30+
public static void WarnFormat(this ILogger l, string msg, params object[] args) => FilteredLog(l, LogLevel.Warning, null, msg, args);
31+
public static void WarnFormat(this ILogger l, Exception ex, string msg, params object[] args) => FilteredLog(l, LogLevel.Warning, ex, msg, args);
3832

39-
public static void Error(this ILogger logger, Exception exception)
40-
{
41-
FilteredLog(logger, LogLevel.Error, exception.ToAllMessages(), null, exception);
42-
}
33+
public static void Error(this ILogger l, string msg) => FilteredLog(l, LogLevel.Error, null, msg, null);
34+
public static void Error(this ILogger l, Func<string> msgFactory) => FilteredLog(l, LogLevel.Error, null, msgFactory);
35+
public static void Error(this ILogger l, Exception ex) => FilteredLog(l, LogLevel.Error, ex, null, null);
36+
public static void Error(this ILogger l, Exception ex, string msg) => FilteredLog(l, LogLevel.Error, ex, msg, null);
37+
public static void ErrorFormat(this ILogger l, string msg, params object[] args) => FilteredLog(l, LogLevel.Error, null, msg, args);
38+
public static void ErrorFormat(this ILogger l, Exception ex, string msg, params object[] args) => FilteredLog(l, LogLevel.Error, ex, msg, args);
39+
40+
public static void Fatal(this ILogger l, string msg) => FilteredLog(l, LogLevel.Fatal, null, msg, null);
41+
public static void Fatal(this ILogger l, Func<string> msgFactory) => FilteredLog(l, LogLevel.Fatal, null, msgFactory);
42+
public static void Fatal(this ILogger l, Exception ex) => FilteredLog(l, LogLevel.Fatal, ex, null, null);
43+
public static void Fatal(this ILogger l, Exception ex, string msg) => FilteredLog(l, LogLevel.Fatal, ex, msg, null);
44+
public static void FatalFormat(this ILogger l, string msg, params object[] args) => FilteredLog(l, LogLevel.Fatal, null, msg, args);
45+
public static void FatalFormat(this ILogger l, Exception ex, string msg, params object[] args) => FilteredLog(l, LogLevel.Fatal, ex, msg, args);
4346

4447
public static void ErrorsAll(this ILogger logger, Exception exception)
4548
{
4649
while (exception != null)
4750
{
48-
FilteredLog(logger, LogLevel.Error, exception.Message, exception.StackTrace, exception);
51+
FilteredLog(logger, LogLevel.Error, exception, exception.Message, null);
4952
exception = exception.InnerException;
5053
}
5154
}
5255

53-
private static void FilteredLog(ILogger logger, LogLevel level, string message, string fullMessage, Exception exception = null)
54-
{
55-
// don't log thread abort exception
56-
if ((exception != null) && (exception is System.Threading.ThreadAbortException))
57-
return;
58-
59-
if (logger.IsEnabledFor(level))
60-
{
61-
if (exception != null && fullMessage.IsEmpty())
62-
{
63-
fullMessage = "{0}\n{1}\n{2}".FormatCurrent(exception.Message, new String('-', 20), exception.StackTrace);
64-
}
56+
private static void FilteredLog(ILogger logger, LogLevel level, Exception exception, string message, object[] objects)
57+
{
58+
if (logger.IsEnabledFor(level))
59+
{
60+
logger.Log(level, exception, message, objects);
61+
}
62+
}
6563

66-
logger.Log(level, message, fullMessage);
67-
}
68-
}
69-
}
64+
private static void FilteredLog(ILogger logger, LogLevel level, Exception exception, Func<string> messageFactory)
65+
{
66+
if (logger.IsEnabledFor(level))
67+
{
68+
logger.Log(level, exception, messageFactory(), null);
69+
}
70+
}
71+
}
7072
}

src/Libraries/SmartStore.Core/Logging/NullLogger.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public bool IsEnabledFor(LogLevel level)
2121
return false;
2222
}
2323

24-
public void Log(LogLevel logLevel, string shortMessage, string fullMessage = "")
25-
{
26-
}
24+
public void Log(LogLevel level, Exception exception, string message, object[] args)
25+
{
26+
}
2727

2828
public void Flush()
2929
{

src/Libraries/SmartStore.Core/Logging/TraceLogger.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,18 @@ public bool IsEnabledFor(LogLevel level)
6464
return true;
6565
}
6666

67-
public void Log(LogLevel logLevel, string shortMessage, string fullMessage = "")
67+
public void Log(LogLevel level, Exception exception, string message, object[] args)
6868
{
69-
var type = LogLevelToEventType(logLevel);
70-
var msg = shortMessage.Grow(fullMessage, Environment.NewLine);
69+
var type = LogLevelToEventType(level);
7170

72-
if (msg.HasValue())
71+
if (exception != null && !exception.IsFatal())
7372
{
74-
_traceSource.TraceEvent(type, (int)type, "{0}: {1}".FormatCurrent(type.ToString().ToUpper(), msg));
73+
message = message.Grow(exception.ToAllMessages(), Environment.NewLine);
74+
}
75+
76+
if (message.HasValue())
77+
{
78+
_traceSource.TraceEvent(type, (int)type, "{0}: {1}".FormatCurrent(type.ToString().ToUpper(), message));
7579
}
7680
}
7781

src/Libraries/SmartStore.Core/Logging/log4net/Log4netLogger.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Globalization;
45
using System.Linq;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using System.Web;
910
using log4net;
1011
using log4net.Core;
12+
using log4net.Util;
1113
using SmartStore.Core.Domain.Customers;
1214

1315
namespace SmartStore.Core.Logging
@@ -36,22 +38,25 @@ public bool IsEnabledFor(LogLevel level)
3638
return _logger.IsEnabledFor(ConvertLevel(level));
3739
}
3840

39-
public void Log(LogLevel logLevel, string shortMessage, string fullMessage = "")
41+
public void Log(LogLevel level, Exception exception, string message, object[] args)
4042
{
41-
var level = ConvertLevel(logLevel);
43+
var logLevel = ConvertLevel(level);
4244

43-
object messageObj = shortMessage;
45+
if (message == null && exception != null)
46+
{
47+
message = exception.Message;
48+
}
4449

45-
// TODO: refactor input
50+
object messageObj = message;
4651

47-
//if (args != null && args.Length > 0)
48-
//{
49-
// messageObj = new SystemStringFormat(CultureInfo.CurrentUICulture, message, args);
50-
//}
52+
if (args != null && args.Length > 0)
53+
{
54+
messageObj = new SystemStringFormat(CultureInfo.CurrentUICulture, message, args);
55+
}
5156

5257
TryAddExtendedThreadInfo();
5358

54-
_logger.Log(_declaringType, level, messageObj, null);
59+
_logger.Log(_declaringType, logLevel, messageObj, exception);
5560
}
5661

5762
protected internal void TryAddExtendedThreadInfo()

src/Libraries/SmartStore.Core/Packaging/NuGet/NugetLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public void Log(MessageLevel level, string message, params object[] args)
2525
_logger.Error(String.Format(message, args));
2626
break;
2727
case MessageLevel.Info:
28-
_logger.Information(String.Format(message, args));
28+
_logger.Info(String.Format(message, args));
2929
break;
3030
case MessageLevel.Warning:
31-
_logger.Warning(String.Format(message, args));
31+
_logger.Warn(String.Format(message, args));
3232
break;
3333
}
3434
}

src/Libraries/SmartStore.Core/Packaging/Updater/AppUpdater.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal bool TryUpdateFromPackage()
9191
}
9292
catch (Exception ex)
9393
{
94-
_logger.Error("An error occured while updating the application: {0}".FormatCurrent(ex.Message), ex);
94+
_logger.Error(ex, "An error occured while updating the application");
9595
return false;
9696
}
9797
}
@@ -124,7 +124,7 @@ private IPackage FindPackage(bool createLogger, out string path)
124124
if (createLogger)
125125
{
126126
_logger = CreateLogger(package);
127-
_logger.Information("Found update package '{0}'".FormatInvariant(package.GetFullName()));
127+
_logger.Info("Found update package '{0}'".FormatInvariant(package.GetFullName()));
128128
}
129129
return package;
130130
}
@@ -168,15 +168,15 @@ private void Backup()
168168
if (localTempPath == null)
169169
{
170170
var exception = new SmartException("Too many backups in '{0}'.".FormatInvariant(tempPath));
171-
_logger.Error(exception.Message, exception);
171+
_logger.Error(exception);
172172
throw exception;
173173
}
174174

175175
var backupFolder = new DirectoryInfo(localTempPath);
176176
var folderUpdater = new FolderUpdater(_logger);
177177
folderUpdater.Backup(source, backupFolder, "App_Data", "Media");
178178

179-
_logger.Information("Backup successfully created in folder '{0}'.".FormatInvariant(localTempPath));
179+
_logger.Info("Backup successfully created in folder '{0}'.".FormatInvariant(localTempPath));
180180
}
181181

182182
private PackageInfo ExecuteUpdate(IPackage package)
@@ -208,7 +208,7 @@ private PackageInfo ExecuteUpdate(IPackage package)
208208
Path = appPath
209209
};
210210

211-
_logger.Information("Update '{0}' successfully executed.".FormatInvariant(info.Name));
211+
_logger.Info("Update '{0}' successfully executed.".FormatInvariant(info.Name));
212212

213213
return info;
214214
}

src/Libraries/SmartStore.Core/Utilities/FileDownloadManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private async Task ProcessUrl(FileDownloadManagerContext context, HttpClient cli
184184
item.ExceptionStatus = webExc.Status;
185185

186186
if (context.Logger != null)
187-
context.Logger.Error(item.ToString(), exception);
187+
context.Logger.Error(exception, item.ToString());
188188
}
189189
catch { }
190190
}

src/Libraries/SmartStore.Data/Setup/DbSeedingMigrator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public int RunPendingMigrations(TContext context)
148148
// Apply external data seeders
149149
RunSeeders<TContext>(externalSeeders, context);
150150

151-
Logger.Information("Database migration successful: {0} >> {1}".FormatInvariant(initialMigration, lastSuccessfulMigration));
151+
Logger.Info("Database migration successful: {0} >> {1}".FormatInvariant(initialMigration, lastSuccessfulMigration));
152152

153153
return result;
154154
}
@@ -171,14 +171,14 @@ private void RunSeeders<T>(IEnumerable<SeederEntry> seederEntries, T ctx) where
171171
throw new DbMigrationException(seederEntry.PreviousMigrationId, seederEntry.MigrationId, ex.InnerException ?? ex, true);
172172
}
173173

174-
Logger.Warning("Seed error in migration '{0}'. The error was ignored because no rollback was requested.".FormatInvariant(seederEntry.MigrationId), ex);
174+
Logger.WarnFormat(ex, "Seed error in migration '{0}'. The error was ignored because no rollback was requested.", seederEntry.MigrationId);
175175
}
176176
}
177177
}
178178

179179
private void LogError(string initialMigration, string targetMigration, Exception exception)
180180
{
181-
Logger.Error("Database migration error: {0} >> {1}".FormatInvariant(initialMigration, targetMigration), exception);
181+
Logger.ErrorFormat(exception, "Database migration error: {0} >> {1}", initialMigration, targetMigration);
182182
}
183183

184184
private class SeederEntry

0 commit comments

Comments
 (0)