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

LogFactory - Ensure to flush and close on shutdown #2010

Merged
merged 1 commit into from
Mar 12, 2017
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
35 changes: 22 additions & 13 deletions src/NLog/LogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public class LogFactory : IDisposable
public event EventHandler<LoggingConfigurationReloadedEventArgs> ConfigurationReloaded;
#endif

private static event EventHandler<EventArgs> LoggerShutdown;

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
/// <summary>
/// Initializes static members of the LogManager class.
Expand All @@ -116,7 +118,7 @@ public LogFactory()
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
this.watcher = new MultiFileWatcher();
this.watcher.FileChanged += this.ConfigFileChanged;
CurrentAppDomain.DomainUnload += DomainUnload;
LoggerShutdown += OnStopLogging;
#endif
}

Expand Down Expand Up @@ -921,6 +923,7 @@ private void Close(TimeSpan flushTimeout)
this.IsDisposing = true;

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
LoggerShutdown -= OnStopLogging;
this.ConfigurationReloaded = null; // Release event listeners

if (this.watcher != null)
Expand Down Expand Up @@ -1409,8 +1412,8 @@ private static void RegisterEvents(IAppDomain appDomain)

try
{
appDomain.ProcessExit += OnStopLogging;
appDomain.DomainUnload += OnStopLogging;
appDomain.ProcessExit += OnLoggerShutdown;
appDomain.DomainUnload += OnLoggerShutdown;
}
catch (Exception exception)
{
Expand All @@ -1427,22 +1430,28 @@ private static void UnregisterEvents(IAppDomain appDomain)
{
if (appDomain == null) return;

appDomain.DomainUnload -= OnStopLogging;
appDomain.ProcessExit -= OnStopLogging;
appDomain.DomainUnload -= OnLoggerShutdown;
appDomain.ProcessExit -= OnLoggerShutdown;
}

private static void OnLoggerShutdown(object sender, EventArgs args)
{
var loggerShutdown = LoggerShutdown;
if (loggerShutdown != null)
loggerShutdown.Invoke(sender, args);
if (currentAppDomain != null)
{
CurrentAppDomain = null; // Unregister and disconnect from AppDomain
}
}

private static void OnStopLogging(object sender, EventArgs args)
private void OnStopLogging(object sender, EventArgs args)
{
try
{
var logFactory = sender as LogFactory;
InternalLogger.Info("Shutting down logging...");
if (logFactory != null)
{
// Finalizer thread has about 2 secs, before being terminated
logFactory.Close(TimeSpan.FromMilliseconds(1500));
}
currentAppDomain = null; // No longer part of AppDomains
// Finalizer thread has about 2 secs, before being terminated
this.Close(TimeSpan.FromMilliseconds(1500));
InternalLogger.Info("Logger has been shut down.");
}
catch (Exception ex)
Expand Down
7 changes: 0 additions & 7 deletions src/NLog/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,6 @@ public static void Shutdown()
factory.Shutdown();
}

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__

#endif

/// <summary>
/// Gets the fully qualified name of the class invoking the LogManager, including the
/// namespace but not the assembly.
Expand Down Expand Up @@ -395,8 +391,5 @@ private static string GetClassFullName()

return className;
}

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
#endif
}
}
8 changes: 4 additions & 4 deletions tests/NLog.UnitTests/Internal/AppDomainPartialTrustTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ public void PartialTrustSuccess(int times, string fileWritePath)
{
var filePath = Path.Combine(fileWritePath, "${level}.txt");


// NOTE Using BufferingWrapper to validate that DomainUnload remembers to perform flush
var configXml = string.Format(@"
<nlog throwExceptions='false'>
<targets async='true'>
<target name='file' type='file' layout='${{message}} ${{threadid}}' filename='{0}' LineEnding='lf' />
<target name='file' type='BufferingWrapper' bufferSize='10000' flushTimeout='15000'>
<target name='filewrapped' type='file' layout='${{message}} ${{threadid}}' filename='{0}' LineEnding='lf' />
</target>
</targets>
<rules>
<logger name='*' minlevel='Debug' appendto='file'>
Expand All @@ -136,8 +138,6 @@ public void PartialTrustSuccess(int times, string fileWritePath)
logger.Error("ddd");
logger.Fatal("eee");
}

LogManager.Flush();
}
}

Expand Down