Skip to content

Added parameter logCategory. #143

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#endregion

using System;
using Common.Logging.Configuration;
using Common.Logging.Factory;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Expand Down Expand Up @@ -123,8 +124,11 @@ private LogWriter GetWriter()
/// Initializes a new instance of the <see cref="EntLibLoggerFactoryAdapter"/> class.
/// </summary>
public EntLibLoggerFactoryAdapter()
: this(EntLibLoggerSettings.DEFAULTPRIORITY, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, null)
{ }
: this(
EntLibLoggerSettings.DEFAULTPRIORITY, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, null,
EntLibLoggerSettings.DEFAULTLOGCATEGORY)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EntLibLoggerFactoryAdapter"/> class
Expand All @@ -133,14 +137,16 @@ public EntLibLoggerFactoryAdapter()
/// <param name="defaultPriority">defaults to <see cref="EntLibLoggerSettings.DEFAULTPRIORITY"/></param>
/// <param name="exceptionFormat">defaults to <see cref="EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT"/></param>
/// <param name="logWriter">a <see cref="LogWriter"/> instance to use</param>
public EntLibLoggerFactoryAdapter(int defaultPriority, string exceptionFormat, LogWriter logWriter)
/// <param name="logCategory">defaults to <see cref="EntLibLoggerSettings.DEFAULTLOGCATEGORY"/></param>
public EntLibLoggerFactoryAdapter(int defaultPriority, string exceptionFormat, LogWriter logWriter,
string logCategory)
: base(true)
{
if (exceptionFormat.Length == 0)
{
exceptionFormat = null;
}
_settings = new EntLibLoggerSettings(defaultPriority, exceptionFormat);
_settings = new EntLibLoggerSettings(defaultPriority, exceptionFormat, logCategory);
_logWriter = logWriter;
}

Expand All @@ -151,17 +157,20 @@ public EntLibLoggerFactoryAdapter(int defaultPriority, string exceptionFormat, L
/// <param name="properties">The properties.</param>
public EntLibLoggerFactoryAdapter(NameValueCollection properties)
: this(ArgUtils.TryParse(EntLibLoggerSettings.DEFAULTPRIORITY, ArgUtils.GetValue(properties, "priority"))
, ArgUtils.Coalesce(ArgUtils.GetValue(properties, "exceptionFormat"), EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT)
, null
, ArgUtils.Coalesce(ArgUtils.GetValue(properties, "exceptionFormat"), EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT)
, null
, ArgUtils.Coalesce(ArgUtils.GetValue(properties, "logCategory"), EntLibLoggerSettings.DEFAULTLOGCATEGORY)
)
{ }
{
}

/// <summary>
/// Creates a new <see cref="EntLibLogger"/> instance.
/// </summary>
protected override ILog CreateLogger(string name)
{
return CreateLogger(name, LogWriter, _settings);
var logCategory = String.IsNullOrWhiteSpace(_settings.logCategory) ? name : _settings.logCategory;
return CreateLogger(logCategory, LogWriter, _settings);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,35 @@ public class EntLibLoggerSettings
/// </remarks>
public static readonly string DEFAULTEXCEPTIONFORMAT = "Exception[ message = $(exception.message), source = $(exception.source), targetsite = $(exception.targetsite), stacktrace = $(exception.stacktrace) ]";

/// <summary>
/// the default log category used for logging in Enterprise Library.
/// </summary>
public static readonly string DEFAULTLOGCATEGORY = "";

/// <summary>
/// the default priority to be used.
/// </summary>
public readonly int priority = DEFAULTPRIORITY;

//format like nlog is better? - i.e. ${exception:format=message,stacktrace:separator=, }
/// <summary>
/// the exception format to be used.
/// </summary>
public readonly string exceptionFormat = DEFAULTEXCEPTIONFORMAT;

/// <summary>
/// the default log category used for logging in Enterprise Library.
/// </summary>
public readonly string logCategory = DEFAULTLOGCATEGORY;

/// <summary>
/// Initializes a new instance.
/// </summary>
public EntLibLoggerSettings(int defaultPriority, string exceptionFormat)
public EntLibLoggerSettings(int defaultPriority, string exceptionFormat, string defaultLogCategory)
{
this.priority = defaultPriority;
this.exceptionFormat = exceptionFormat;
this.logCategory = defaultLogCategory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ private static TestEntLibLoggerFactoryAdapter CreateTestEntLibLoggerFactoryAdapt
, true
);

return new TestEntLibLoggerFactoryAdapter(5, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, logWriter);
return new TestEntLibLoggerFactoryAdapter(5, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, logWriter, EntLibLoggerSettings.DEFAULTLOGCATEGORY);
}

private class TestEntLibLoggerFactoryAdapter: EntLibLoggerFactoryAdapter
{
public LogEntry LastLogEntry;

public TestEntLibLoggerFactoryAdapter(int defaultPriority, string exceptionFormat, LogWriter logWriter)
: base(defaultPriority, exceptionFormat, logWriter)
public TestEntLibLoggerFactoryAdapter(int defaultPriority, string exceptionFormat, LogWriter logWriter, string logCategory)
: base(defaultPriority, exceptionFormat, logWriter, logCategory)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EntLibTests : ILogTestsBase
protected override ILoggerFactoryAdapter GetLoggerFactoryAdapter()
{
LogWriter lw = new LogWriter(new List<ILogFilter>(), new List<LogSource>(), new LogSource("errorSource"), "default");
return new EntLibLoggerFactoryAdapter(EntLibLoggerSettings.DEFAULTPRIORITY, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, lw);
return new EntLibLoggerFactoryAdapter(EntLibLoggerSettings.DEFAULTPRIORITY, EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT, lw, EntLibLoggerSettings.DEFAULTLOGCATEGORY);
}

[Test]
Expand Down