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

Also use logFactory for ThrowConfigExceptions #1299

Merged
merged 2 commits into from
Mar 21, 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
4 changes: 2 additions & 2 deletions src/NLog/Config/XmlLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ private void ParseNLogElement(NLogXmlElement nlogElement, string filePath, bool
this.fileMustAutoReloadLookup[GetFileLookupKey(filePath)] = autoReload;

logFactory.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", logFactory.ThrowExceptions);
LogManager.ThrowConfigExceptions = nlogElement.GetOptionalBooleanAttribute("throwConfigExceptions", LogManager.ThrowConfigExceptions);
logFactory.ThrowConfigExceptions = nlogElement.GetOptionalBooleanAttribute("throwConfigExceptions", logFactory.ThrowConfigExceptions);
InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole);
InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError);
InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile);
Expand Down Expand Up @@ -1190,4 +1190,4 @@ private string ExpandSimpleVariables(string input)
return output;
}
}
}
}
21 changes: 7 additions & 14 deletions src/NLog/Internal/TargetWithFilterChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System.Linq;

namespace NLog.Internal
{
using System.Collections.Generic;
Expand All @@ -43,8 +45,8 @@ namespace NLog.Internal
/// whether logging should happen.
/// </summary>
[NLogConfigurationItem]
internal class TargetWithFilterChain
{
internal class TargetWithFilterChain
{
private StackTraceUsage stackTraceUsage = StackTraceUsage.None;

/// <summary>
Expand Down Expand Up @@ -92,19 +94,10 @@ internal void PrecalculateStackTraceUsage()

// find all objects which may need stack trace
// and determine maximum
foreach (var item in ObjectGraphScanner.FindReachableObjects<IUsesStackTrace>(this))
// only the target can have IUsesStackTrace
if (Target != null)
{
var stu = item.StackTraceUsage;

if (stu > this.stackTraceUsage)
{
this.stackTraceUsage = stu;

if (this.stackTraceUsage >= StackTraceUsage.Max)
{
break;
}
}
this.stackTraceUsage = Target.GetAllLayouts().OfType<IUsesStackTrace>().DefaultIfEmpty().Max(usage => usage == null ? StackTraceUsage.None : usage.StackTraceUsage);
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/NLog/Layouts/SimpleLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System.Collections.Generic;
using System.Linq;

namespace NLog.Layouts
{
using System;
Expand All @@ -51,7 +54,7 @@ namespace NLog.Layouts
[Layout("SimpleLayout")]
[ThreadAgnostic]
[AppDomainFixedOutput]
public class SimpleLayout : Layout
public class SimpleLayout : Layout, IUsesStackTrace
{
private const int MaxInitialRenderBufferLength = 16384;
private int maxRenderedLength;
Expand Down Expand Up @@ -146,6 +149,13 @@ public string FixedText
/// </summary>
public ReadOnlyCollection<LayoutRenderer> Renderers { get; private set; }


/// <summary>
/// Gets the level of stack trace information required for rendering.
/// </summary>
/// <remarks>Calculated when setting <see cref="Renderers"/>.</remarks>
public StackTraceUsage StackTraceUsage { get; private set; }

/// <summary>
/// Converts a text to a simple layout.
/// </summary>
Expand Down Expand Up @@ -208,17 +218,19 @@ public override string ToString()
{
return "'" + this.Text + "'";
}

internal void SetRenderers(LayoutRenderer[] renderers, string text)
{
this.Renderers = new ReadOnlyCollection<LayoutRenderer>(renderers);
if (this.Renderers.Count == 1 && this.Renderers[0] is LiteralLayoutRenderer)
{
this.fixedText = ((LiteralLayoutRenderer)this.Renderers[0]).Text;
this.StackTraceUsage = StackTraceUsage.None;
}
else
{
this.fixedText = null;
this.StackTraceUsage = this.Renderers.OfType<IUsesStackTrace>().DefaultIfEmpty().Max(usage => usage == null ? StackTraceUsage.None : usage.StackTraceUsage);
}

this.layoutText = text;
Expand Down Expand Up @@ -287,5 +299,7 @@ protected override string GetFormattedMessage(LogEventInfo logEvent)
logEvent.AddCachedLayoutValue(this, value);
return value;
}


}
}
22 changes: 22 additions & 0 deletions src/NLog/Targets/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class Target : ISupportsInitialize, IDisposable
{
private object lockObject = new object();
private List<Layout> allLayouts;
private bool scannedForLayouts;
private Exception initializeException;

/// <summary>
Expand All @@ -76,6 +77,20 @@ protected object SyncRoot
/// </summary>
protected bool IsInitialized { get; private set; }

/// <summary>
/// Get all used layouts in this target.
/// </summary>
/// <returns></returns>
internal List<Layout> GetAllLayouts()
{

if (!scannedForLayouts)
{
FindAllLayouts();
}
return allLayouts;
}

/// <summary>
/// Initializes this instance.
/// </summary>
Expand Down Expand Up @@ -396,9 +411,16 @@ protected virtual void Dispose(bool disposing)
/// to initialize logging.
/// </summary>
protected virtual void InitializeTarget()
{
//rescan as amount layouts can be changed.
FindAllLayouts();
}

private void FindAllLayouts()
{
this.allLayouts = new List<Layout>(ObjectGraphScanner.FindReachableObjects<Layout>(this));
InternalLogger.Trace("{0} has {1} layouts", this, this.allLayouts.Count);
this.scannedForLayouts = true;
}

/// <summary>
Expand Down