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

Expose logger properties on Logger #3424

Closed
wants to merge 3 commits into from
Closed
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
51 changes: 30 additions & 21 deletions src/NLog/Logger.cs
Expand Up @@ -82,6 +82,15 @@ protected internal Logger()
/// </summary>
public LogFactory Factory { get; private set; }

#if !NET3_5 && !NET4_0 && !SILVERLIGHT && !WINDOWS_PHONE
304NotModified marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Properties added with <see cref="WithProperty"/> or <see cref="SetProperty"/>
/// </summary>
public IReadOnlyDictionary<string, object> Properties => CreateContextPropertiesDictionary(_contextProperties); //clone for immutable

#endif

/// <summary>
/// Gets a value indicating whether logging is enabled for the specified level.
/// </summary>
Expand Down Expand Up @@ -110,7 +119,8 @@ public Logger WithProperty(string propertyKey, object propertyValue)

Logger newLogger = Factory.CreateNewLogger(GetType()) ?? new Logger();
newLogger.Initialize(Name, _configuration, Factory);
newLogger._contextProperties = CopyOnWrite(propertyKey, propertyValue);
newLogger._contextProperties = CreateContextPropertiesDictionary(_contextProperties);
newLogger._contextProperties[propertyKey] = propertyValue;
newLogger._contextLogger = _contextLogger; // Use the LoggerConfiguration of the parent Logger
return newLogger;
}
Expand All @@ -128,16 +138,15 @@ public void SetProperty(string propertyKey, object propertyValue)
if (string.IsNullOrEmpty(propertyKey))
throw new ArgumentException(nameof(propertyKey));

_contextProperties = CopyOnWrite(propertyKey, propertyValue);
_contextProperties = CreateContextPropertiesDictionary(_contextProperties);
_contextProperties[propertyKey] = propertyValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not good. As you are modifying the active dictionary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, CreateContextPropertiesDictionary made a new reference. But anyway, I see this PR is already superseded 👼

}

private Dictionary<string, object> CopyOnWrite(string propertyKey, object propertyValue)
private static Dictionary<string, object> CreateContextPropertiesDictionary(Dictionary<string, object> contextProperties)
{
var contextProperties = _contextProperties;
contextProperties = contextProperties != null
? new Dictionary<string, object>(contextProperties)
: new Dictionary<string, object>();
contextProperties[propertyKey] = propertyValue;
return contextProperties;
}

Expand Down Expand Up @@ -246,10 +255,10 @@ public void LogException(LogLevel level, [Localizable(false)] string message, Ex
/// <param name="args">Arguments to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, params object[] args)
{
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, args);
WriteToTargets(level, formatProvider, message, args);
}
}

Expand All @@ -258,8 +267,8 @@ public void Log(LogLevel level, IFormatProvider formatProvider, [Localizable(fal
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">Log message.</param>
public void Log(LogLevel level, [Localizable(false)] string message)
{
public void Log(LogLevel level, [Localizable(false)] string message)
{
if (IsEnabled(level))
{
WriteToTargets(level, null, message);
Expand All @@ -273,8 +282,8 @@ public void Log(LogLevel level, [Localizable(false)] string message)
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log(LogLevel level, [Localizable(false)] string message, params object[] args)
{
public void Log(LogLevel level, [Localizable(false)] string message, params object[] args)
{
if (IsEnabled(level))
{
WriteToTargets(level, message, args);
Expand Down Expand Up @@ -340,10 +349,10 @@ public void Log(LogLevel level, Exception exception, IFormatProvider formatProvi
/// <param name="argument">The argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument argument)
{
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, new object[] { argument });
WriteToTargets(level, formatProvider, message, new object[] { argument });
}
}

Expand Down Expand Up @@ -374,11 +383,11 @@ public void Log<TArgument>(LogLevel level, [Localizable(false)] string message,
/// <param name="argument1">The first argument to format.</param>
/// <param name="argument2">The second argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2)
{
public void Log<TArgument1, TArgument2>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, new object[] { argument1, argument2 });
WriteToTargets(level, formatProvider, message, new object[] { argument1, argument2 });
}
}

Expand All @@ -393,7 +402,7 @@ public void Log<TArgument>(LogLevel level, [Localizable(false)] string message,
/// <param name="argument2">The second argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2>(LogLevel level, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2)
{
{
if (IsEnabled(level))
{
WriteToTargets(level, message, new object[] { argument1, argument2 });
Expand All @@ -413,11 +422,11 @@ public void Log<TArgument>(LogLevel level, [Localizable(false)] string message,
/// <param name="argument2">The second argument to format.</param>
/// <param name="argument3">The third argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2, TArgument3>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2, TArgument3 argument3)
{
public void Log<TArgument1, TArgument2, TArgument3>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2, TArgument3 argument3)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, new object[] { argument1, argument2, argument3 });
WriteToTargets(level, formatProvider, message, new object[] { argument1, argument2, argument3 });
}
}

Expand All @@ -434,7 +443,7 @@ public void Log<TArgument>(LogLevel level, [Localizable(false)] string message,
/// <param name="argument3">The third argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2, TArgument3>(LogLevel level, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2, TArgument3 argument3)
{
{
if (IsEnabled(level))
{
WriteToTargets(level, message, new object[] { argument1, argument2, argument3 });
Expand Down