Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WithProperty and SetProperty on Logger #3298

Merged
merged 3 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 3 additions & 21 deletions src/NLog/Internal/LoggerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ internal class LoggerConfiguration
/// Initializes a new instance of the <see cref="LoggerConfiguration" /> class.
/// </summary>
/// <param name="targetsByLevel">The targets by level.</param>
/// <param name="exceptionLoggingOldStyle"> Use the old exception log handling of NLog 3.0?
/// </param>
public LoggerConfiguration(TargetWithFilterChain[] targetsByLevel, bool exceptionLoggingOldStyle = false)
/// <param name="exceptionLoggingOldStyle"> Use the old exception log handling of NLog 3.0?</param>
public LoggerConfiguration(TargetWithFilterChain[] targetsByLevel, bool exceptionLoggingOldStyle)
{
_targetsByLevel = targetsByLevel;
#pragma warning disable 618
Expand All @@ -61,7 +60,7 @@ public LoggerConfiguration(TargetWithFilterChain[] targetsByLevel, bool exceptio
/// </summary>
/// <remarks>This method was marked as obsolete before NLog 4.3.11 and it will be removed in NLog 5.</remarks>
[Obsolete("This property marked obsolete before v4.3.11 and it will be removed in NLog 5.")]
public bool ExceptionLoggingOldStyle { get; private set; }
public bool ExceptionLoggingOldStyle { get; }

/// <summary>
/// Gets targets for the specified level.
Expand All @@ -74,24 +73,7 @@ public TargetWithFilterChain GetTargetsForLevel(LogLevel level)
{
return null;
}

return _targetsByLevel[level.Ordinal];
}

/// <summary>
/// Determines whether the specified level is enabled.
/// </summary>
/// <param name="level">The level.</param>
/// <returns>
/// A value of <c>true</c> if the specified level is enabled; otherwise, <c>false</c>.
/// </returns>
public bool IsEnabled(LogLevel level)
{
if (level == LogLevel.Off)
{
return false;
}
return _targetsByLevel[level.Ordinal] != null;
}
}
}
91 changes: 40 additions & 51 deletions src/NLog/LogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public Logger GetCurrentClassLogger(Type loggerType)
#else
var className = StackTraceUsageUtils.GetClassFullName(new StackFrame(1, false));
#endif
return GetLogger(className, loggerType);
return GetLoggerThreadSafe(className, loggerType);
}

/// <summary>
Expand All @@ -392,7 +392,7 @@ public Logger GetCurrentClassLogger(Type loggerType)
/// are not guaranteed to return the same logger reference.</returns>
public Logger GetLogger(string name)
{
return GetLogger(new LoggerCacheKey(name, typeof(Logger)));
return GetLoggerThreadSafe(name, Logger.DefaultLoggerType);
}

/// <summary>
Expand All @@ -404,7 +404,7 @@ public Logger GetLogger(string name)
/// are not guaranteed to return the same logger reference.</returns>
public T GetLogger<T>(string name) where T : Logger
{
return (T)GetLogger(new LoggerCacheKey(name, typeof(T)));
return (T)GetLoggerThreadSafe(name, typeof(T));
}

/// <summary>
Expand All @@ -416,7 +416,7 @@ public Logger GetLogger(string name)
/// same argument aren't guaranteed to return the same logger reference.</returns>
public Logger GetLogger(string name, Type loggerType)
{
return GetLogger(new LoggerCacheKey(name, loggerType));
return GetLoggerThreadSafe(name, loggerType);
}

/// <summary>
Expand All @@ -431,7 +431,6 @@ public void ReconfigExistingLoggers()
lock (_syncRoot)
{
_config?.InitializeAll();

loggers = _loggerCache.GetLoggers();
}

Expand Down Expand Up @@ -739,8 +738,9 @@ internal LoggerConfiguration GetConfigurationForLogger(string name, LoggingConfi
}

#pragma warning disable 618
return new LoggerConfiguration(targetsByLevel, configuration != null && configuration.ExceptionLoggingOldStyle);
var exceptionLoggingOldStyle = configuration?.ExceptionLoggingOldStyle == true;
#pragma warning restore 618
return new LoggerConfiguration(targetsByLevel, exceptionLoggingOldStyle);
}

/// <summary>
Expand Down Expand Up @@ -887,8 +887,10 @@ public void ResetCandidateConfigFilePath()
_candidateConfigFilePaths = null;
}

private Logger GetLogger(LoggerCacheKey cacheKey)
private Logger GetLoggerThreadSafe(string name, Type loggerType)
{
LoggerCacheKey cacheKey = new LoggerCacheKey(name, loggerType ?? typeof(Logger));

lock (_syncRoot)
{
Logger existingLogger = _loggerCache.Retrieve(cacheKey);
Expand All @@ -898,49 +900,44 @@ private Logger GetLogger(LoggerCacheKey cacheKey)
return existingLogger;
}

Logger newLogger;
if (cacheKey.ConcreteType != null && cacheKey.ConcreteType != typeof(Logger))
Logger newLogger = CreateNewLogger(cacheKey.ConcreteType);
if (newLogger == null)
{
try
{
newLogger = CreateCustomLoggerType(cacheKey.ConcreteType);
if (newLogger == null)
{
// Creating default instance of logger if instance of specified type cannot be created.
newLogger = CreateDefaultLogger(ref cacheKey);
}
}
catch (Exception ex)
{
InternalLogger.Error(ex, "GetLogger / GetCurrentClassLogger. Cannot create instance of type '{0}'. It should have an default contructor.", cacheKey.ConcreteType);
cacheKey = new LoggerCacheKey(cacheKey.Name, typeof(Logger));
newLogger = new Logger();
}

if (ex.MustBeRethrown())
{
throw;
}
newLogger.Initialize(name, GetConfigurationForLogger(name, Configuration), this);
_loggerCache.InsertOrUpdate(cacheKey, newLogger);
return newLogger;
}
}

// Creating default instance of logger if instance of specified type cannot be created.
newLogger = CreateDefaultLogger(ref cacheKey);
}
}
else
internal Logger CreateNewLogger(Type loggerType)
{
Logger newLogger;
if (loggerType != null && loggerType != typeof(Logger))
{
try
{
newLogger = new Logger();
newLogger = CreateCustomLoggerType(loggerType);
}

if (cacheKey.ConcreteType != null)
catch (Exception ex)
{
newLogger.Initialize(cacheKey.Name, GetConfigurationForLogger(cacheKey.Name, Configuration), this);
InternalLogger.Error(ex, "GetLogger / GetCurrentClassLogger. Cannot create instance of type '{0}'. It should have an default contructor.", loggerType);
if (ex.MustBeRethrown())
{
throw;
}
newLogger = null;
}

// TODO: Clarify what is the intention when cacheKey.ConcreteType = null.
// At the moment, a logger typeof(Logger) will be created but the ConcreteType
// will remain null and inserted into the cache.
// Should we set cacheKey.ConcreteType = typeof(Logger) for default loggers?

_loggerCache.InsertOrUpdate(cacheKey, newLogger);
return newLogger;
}
else
{
newLogger = new Logger();
}

return newLogger;
}

private Logger CreateCustomLoggerType(Type customLoggerType)
Expand Down Expand Up @@ -978,14 +975,6 @@ private Logger CreateCustomLoggerType(Type customLoggerType)
}
}

private static Logger CreateDefaultLogger(ref LoggerCacheKey cacheKey)
{
cacheKey = new LoggerCacheKey(cacheKey.Name, typeof(Logger));

var newLogger = new Logger();
return newLogger;
}

/// <summary>
/// Loads logging configuration from file (Currently only XML configuration files supported)
/// </summary>
Expand All @@ -1004,7 +993,7 @@ public LogFactory LoadConfiguration(string configFile)
/// <summary>
/// Logger cache key.
/// </summary>
internal struct LoggerCacheKey : IEquatable<LoggerCacheKey>
private struct LoggerCacheKey : IEquatable<LoggerCacheKey>
{
public readonly string Name;
public readonly Type ConcreteType;
Expand Down
12 changes: 6 additions & 6 deletions src/NLog/Logger-generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public partial class Logger
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Trace</c> level, otherwise it returns <see langword="false" />.</returns>
public bool IsTraceEnabled
{
get { return _isTraceEnabled; }
get { return _contextLogger._isTraceEnabled; }
}

/// <summary>
Expand All @@ -57,7 +57,7 @@ public bool IsTraceEnabled
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Debug</c> level, otherwise it returns <see langword="false" />.</returns>
public bool IsDebugEnabled
{
get { return _isDebugEnabled; }
get { return _contextLogger._isDebugEnabled; }
}

/// <summary>
Expand All @@ -66,7 +66,7 @@ public bool IsDebugEnabled
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Info</c> level, otherwise it returns <see langword="false" />.</returns>
public bool IsInfoEnabled
{
get { return _isInfoEnabled; }
get { return _contextLogger._isInfoEnabled; }
}

/// <summary>
Expand All @@ -75,7 +75,7 @@ public bool IsInfoEnabled
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Warn</c> level, otherwise it returns <see langword="false" />.</returns>
public bool IsWarnEnabled
{
get { return _isWarnEnabled; }
get { return _contextLogger._isWarnEnabled; }
}

/// <summary>
Expand All @@ -84,7 +84,7 @@ public bool IsWarnEnabled
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Error</c> level, otherwise it returns <see langword="false" />.</returns>
public bool IsErrorEnabled
{
get { return _isErrorEnabled; }
get { return _contextLogger._isErrorEnabled; }
}

/// <summary>
Expand All @@ -93,7 +93,7 @@ public bool IsErrorEnabled
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Fatal</c> level, otherwise it returns <see langword="false" />.</returns>
public bool IsFatalEnabled
{
get { return _isFatalEnabled; }
get { return _contextLogger._isFatalEnabled; }
}


Expand Down
4 changes: 2 additions & 2 deletions src/NLog/Logger-generated.tt
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ namespace NLog
var levels = new string[]{"Trace", "Debug", "Info", "Warn", "Error", "Fatal"};

foreach(var level in levels)
{
{
#>
/// <summary>
/// Gets a value indicating whether logging is enabled for the <c><#=level#></c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c><#=level#></c> level, otherwise it returns <see langword="false" />.</returns>
public bool Is<#=level#>Enabled
{
get { return _is<#=level#>Enabled; }
get { return _contextLogger._is<#=level#>Enabled; }
}

<#
Expand Down