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

Callsite: add includeNamespace option #1836

Merged
merged 1 commit into from
Dec 10, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/NLog/LayoutRenderers/CallSiteLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public CallSiteLayoutRenderer()
this.ClassName = true;
this.MethodName = true;
this.CleanNamesOfAnonymousDelegates = false;
this.IncludeNamespace = true;
#if !SILVERLIGHT
this.FileName = false;
this.IncludeSourcePath = true;
Expand All @@ -71,6 +72,13 @@ public CallSiteLayoutRenderer()
[DefaultValue(true)]
public bool ClassName { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to render the include the namespace with <see cref="ClassName"/>.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
[DefaultValue(true)]
public bool IncludeNamespace { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to render the method name.
/// </summary>
Expand Down Expand Up @@ -138,9 +146,10 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
MethodBase method = frame.GetMethod();
if (this.ClassName)
{
if (method.DeclaringType != null)
var type = method.DeclaringType;
if (type != null)
{
string className = method.DeclaringType.FullName;
string className = IncludeNamespace ? type.FullName : type.Name;

if (this.CleanNamesOfAnonymousDelegates)
{
Expand Down
19 changes: 17 additions & 2 deletions tests/NLog.UnitTests/LayoutRenderers/CallSiteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,23 @@ public void ClassNameTest()

ILogger logger = LogManager.GetLogger("A");
logger.Debug("msg");
MethodBase currentMethod = MethodBase.GetCurrentMethod();
AssertDebugLastMessage("debug", currentMethod.DeclaringType.FullName + " msg");
AssertDebugLastMessage("debug", "NLog.UnitTests.LayoutRenderers.CallSiteTests msg");
}

[Fact]
public void ClassNameTestWithoutNamespace()
{
LogManager.Configuration = CreateConfigurationFromString(@"
<nlog>
<targets><target name='debug' type='Debug' layout='${callsite:classname=true:methodname=false:includeNamespace=false} ${message}' /></targets>
<rules>
<logger name='*' minlevel='Debug' writeTo='debug' />
</rules>
</nlog>");

ILogger logger = LogManager.GetLogger("A");
logger.Debug("msg");
AssertDebugLastMessage("debug", "CallSiteTests msg");
}

[Fact]
Expand Down