public void ImpersonatingWrapperTest()
        {
            var wrapped = new MyTarget()
            {
                ExpectedUser = Environment.MachineName + "\\" + NLogTestUser,
            };

            var wrapper = new ImpersonatingTargetWrapper()
            {
                UserName      = NLogTestUser,
                Password      = NLogTestUserPassword,
                Domain        = Environment.MachineName,
                WrappedTarget = wrapped,
            };

            // wrapped.Initialize(null);
            wrapper.Initialize(null);

            var exceptions = new List <Exception>();

            wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Single(exceptions);
            wrapper.WriteAsyncLogEvents(
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Equal(4, exceptions.Count);
            wrapper.Flush(exceptions.Add);
            Assert.Equal(5, exceptions.Count);
            foreach (var ex in exceptions)
            {
                Assert.Null(ex);
            }

            wrapper.Close();
        }
        public void ImpersonatingWrapperNegativeTest()
        {
            var wrapped = new MyTarget()
            {
                ExpectedUser = NLogTestUser,
            };

            LogManager.ThrowExceptions = true;

            var wrapper = new ImpersonatingTargetWrapper()
            {
                UserName      = NLogTestUser,
                Password      = Guid.NewGuid().ToString("N"), // wrong password
                Domain        = Environment.MachineName,
                WrappedTarget = wrapped,
            };

            Assert.Throws <COMException>(() =>
            {
                wrapper.Initialize(null);
            });

            wrapper.Close(); // will not fail because Initialize() failed
        }
        public void RevertToSelfTest()
        {
            CreateUserIfNotPresent();

            var wrapped = new MyTarget()
            {
                ExpectedUser = LocalMachineName + "\\" + Environment.UserName,
            };

            WindowsIdentity originalIdentity = WindowsIdentity.GetCurrent();

            var newIdentity = new ImpersonatingTargetWrapper.NewIdentityHandle(
                NLogTestUser,
                LocalMachineName,
                NLogTestUserPassword,
                SecurityLogOnType.Interactive,
                LogOnProviderType.Default,
                SecurityImpersonationLevel.Identification
                );

            try
            {
                ImpersonatingTargetWrapper.NewIdentityHandle.RunImpersonated(newIdentity, (s) =>
                {
                    WindowsIdentity changedIdentity = WindowsIdentity.GetCurrent();
                    Assert.Contains(NLogTestUser.ToLowerInvariant(), changedIdentity.Name.ToLowerInvariant(), StringComparison.InvariantCulture);

                    var wrapper = new ImpersonatingTargetWrapper()
                    {
                        WrappedTarget = wrapped,
                        RevertToSelf  = true,
                    };

                    var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
                    {
                        cfg.Configuration.AddRuleForAllLevels(wrapper);
                    }).LogFactory;

                    var exceptions = new List <Exception>();
                    wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
                    Assert.Single(exceptions);
                    wrapper.WriteAsyncLogEvents(
                        LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                        LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                        LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
                    Assert.Equal(4, exceptions.Count);
                    wrapper.Flush(exceptions.Add);
                    Assert.Equal(5, exceptions.Count);
                    foreach (var ex in exceptions)
                    {
                        Assert.Null(ex);
                    }

                    logFactory.Shutdown();
                }, (object)null);
            }
            finally
            {
                newIdentity.Dispose();

                WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                Assert.Equal(originalIdentity.Name.ToLowerInvariant(), currentIdentity.Name.ToLowerInvariant());
            }
        }