NLog.Targets.Target.Close C# (CSharp) Method

Close() private method

Closes this instance.
private Close ( ) : void
return void
        internal void Close()
        {
            lock (this.SyncRoot)
            {
                this.LoggingConfiguration = null;

                if (this.IsInitialized)
                {
                    this.IsInitialized = false;

                    try
                    {
                        if (this.initializeException == null)
                        {
                            // if Init succeeded, call Close()
                            this.CloseTarget();
                        }
                    }
                    catch (Exception exception)
                    {
                        if (exception.MustBeRethrown())
                        {
                            throw;
                        }

                        InternalLogger.Error("Error closing target {0} {1}.", this, exception);
                        throw;
                    }
                }
            }
        }

Usage Example

        private static void AssertOutput(Target target, string message, string[] expectedParts)
        {
            var consoleOutWriter = new PartsWriter();
            TextWriter oldConsoleOutWriter = Console.Out;
            Console.SetOut(consoleOutWriter);

            try
            {
                var exceptions = new List<Exception>();
                target.Initialize(null);
                target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger", message).WithContinuation(exceptions.Add));
                target.Close();

                Assert.Equal(1, exceptions.Count);
                Assert.True(exceptions.TrueForAll(e => e == null));
            }
            finally
            {
                Console.SetOut(oldConsoleOutWriter);
            }

            var expected = Enumerable.Repeat("Logger " + expectedParts[0], 1).Concat(expectedParts.Skip(1));
            Assert.Equal(expected, consoleOutWriter.Values);
        }