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

Initialize() private method

Initializes this instance.
private Initialize ( LoggingConfiguration configuration ) : void
configuration NLog.Config.LoggingConfiguration The configuration.
return void
        internal void Initialize(LoggingConfiguration configuration)
        {
            lock (this.SyncRoot)
            {
                this.LoggingConfiguration = configuration;

                if (!this.IsInitialized)
                {
                    PropertyHelper.CheckRequiredParameters(this);
                    this.IsInitialized = true;
                    try
                    {
                        this.InitializeTarget();
                        this.initializeException = null;
                    }
                    catch (Exception exception)
                    {
                        if (exception.MustBeRethrown())
                        {
                            throw;
                        }

                        this.initializeException = exception;
                        InternalLogger.Error("Error initializing 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);
        }