<commit_before>﻿// 
// Copyright (c) 2004-2011 <NAME> <<EMAIL>>
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer. 
// 
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution. 
// 
// * Neither the name of <NAME> nor the names of its 
//   contributors may be used to endorse or promote products derived from this
//   software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
// 

#if !SILVERLIGHT && !MONO

namespace NLog.UnitTests.Targets
{
    using System.Diagnostics;
    using NLog.Config;
    using NLog.Targets;
    using System;
    using System.Linq;
    using Xunit;
    using System.Collections.Generic;
    using System.Diagnostics.Eventing.Reader;
    using NLog.Layouts;
    using Xunit.Extensions;

    public class EventLogTargetTests : NLogTestBase
    {
        [Fact]
        public void MaxMessageLengthShouldBe16384_WhenNotSpecifyAnyOption()
        {
            const int expectedMaxMessageLength = 16384;
            LoggingConfiguration c = CreateConfigurationFromString(@"
            <nlog ThrowExceptions='true'>
                <targets>
                    <target type='EventLog' name='eventLog1' layout='${message}' />
                </targets>
                <rules>
                      <logger name='*' writeTo='eventLog1'>
                      </logger>
                </rules>
            </nlog>");

            var eventLog1 = c.FindTargetByName<EventLogTarget>("eventLog1");
            Assert.Equal(expectedMaxMessageLength, eventLog1.MaxMessageLength);
        }

        [Fact]
        public void MaxMessageLengthShouldBeAsSpecifiedOption()
        {
            const int expectedMaxMessageLength = 1000;
            LoggingConfiguration c = CreateConfigurationFromString(string.Format(@"
            <nlog ThrowExceptions='true'>
                <targets>
                    <target type='EventLog' name='eventLog1' layout='${{message}}' maxmessagelength='{0}' />
                </targets>
                <rules>
                      <logger name='*' writeTo='eventLog1'>
                      </logger>
                    </rules>
            </nlog>", expectedMaxMessageLength));

            var eventLog1 = c.FindTargetByName<EventLogTarget>("eventLog1");
            Assert.Equal(expectedMaxMessageLength, eventLog1.MaxMessageLength);
        }

        [Theory]
        [InlineData(0)]
        [InlineData(-1)]
        public void ConfigurationShouldThrowException_WhenMaxMessageLengthIsNegativeOrZero(int maxMessageLength)
        {
            string configrationText = string.Format(@"
            <nlog ThrowExceptions='true'>
                <targets>
                    <target type='EventLog' name='eventLog1' layout='${{message}}' maxmessagelength='{0}' />
                </targets>
                <rules>
                      <logger name='*' writeTo='eventLog1'>
                      </logger>
                    </rules>
            </nlog>", maxMessageLength);

            NLogConfigurationException ex = Assert.Throws<NLogConfigurationException>(() => CreateConfigurationFromString(configrationText));
            Assert.Equal("MaxMessageLength cannot be zero or negative.", ex.InnerException.Message);
        }

        [Theory]
        [InlineData(0)]
        [InlineData(-1)]
        public void ShouldThrowException_WhenMaxMessageLengthSetNegativeOrZero(int maxMessageLength)
        {
            ArgumentException ex = Assert.Throws<ArgumentException>(() =>
            {
                var target = new EventLogTarget();
                target.MaxMessageLength = maxMessageLength;
            });

            Assert.Equal("MaxMessageLength cannot be zero or negative.", ex.Message);
        }


        private void AssertMessageAndLogLevelForTruncatedMessages(LogLevel loglevel, EventLogEntryType expectedEventLogEntryType, string expectedMessage, Layout entryTypeLayout)
        {
            const int expectedEntryCount = 1;
            var eventRecords = Write(loglevel, expectedEventLogEntryType, expectedMessage, entryTypeLayout, EventLogTargetOverflowAction.Truncate).ToList();

            Assert.Equal(expectedEntryCount, eventRecords.Count);
            AssertWrittenMessage(eventRecords, expectedMessage);<commit_msg>Fix exceptions (now wrapped)<commit_after>﻿// 
// Copyright (c) 2004-2011 <NAME> <<EMAIL>>
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer. 
// 
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution. 
// 
// * Neither the name of <NAME> nor the names of its 
//   contributors may be used to endorse or promote products derived from this
//   software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
// 

#if !SILVERLIGHT && !MONO

namespace NLog.UnitTests.Targets
{
    using System.Diagnostics;
    using NLog.Config;
    using NLog.Targets;
    using System;
    using System.Linq;
    using Xunit;
    using System.Collections.Generic;
    using System.Diagnostics.Eventing.Reader;
    using NLog.Layouts;
    using Xunit.Extensions;

    public class EventLogTargetTests : NLogTestBase
    {
        [Fact]
        public void MaxMessageLengthShouldBe16384_WhenNotSpecifyAnyOption()
        {
            const int expectedMaxMessageLength = 16384;
            LoggingConfiguration c = CreateConfigurationFromString(@"
            <nlog ThrowExceptions='true'>
                <targets>
                    <target type='EventLog' name='eventLog1' layout='${message}' />
                </targets>
                <rules>
                      <logger name='*' writeTo='eventLog1'>
                      </logger>
                </rules>
            </nlog>");

            var eventLog1 = c.FindTargetByName<EventLogTarget>("eventLog1");
            Assert.Equal(expectedMaxMessageLength, eventLog1.MaxMessageLength);
        }

        [Fact]
        public void MaxMessageLengthShouldBeAsSpecifiedOption()
        {
            const int expectedMaxMessageLength = 1000;
            LoggingConfiguration c = CreateConfigurationFromString(string.Format(@"
            <nlog ThrowExceptions='true'>
                <targets>
                    <target type='EventLog' name='eventLog1' layout='${{message}}' maxmessagelength='{0}' />
                </targets>
                <rules>
                      <logger name='*' writeTo='eventLog1'>
                      </logger>
                    </rules>
            </nlog>", expectedMaxMessageLength));

            var eventLog1 = c.FindTargetByName<EventLogTarget>("eventLog1");
            Assert.Equal(expectedMaxMessageLength, eventLog1.MaxMessageLength);
        }

        [Theory]
        [InlineData(0)]
        [InlineData(-1)]
        public void ConfigurationShouldThrowException_WhenMaxMessageLengthIsNegativeOrZero(int maxMessageLength)
        {
            string configrationText = string.Format(@"
            <nlog ThrowExceptions='true'>
                <targets>
                    <target type='EventLog' name='eventLog1' layout='${{message}}' maxmessagelength='{0}' />
                </targets>
                <rules>
                      <logger name='*' writeTo='eventLog1'>
                      </logger>
                    </rules>
            </nlog>", maxMessageLength);

            NLogConfigurationException ex = Assert.Throws<NLogConfigurationException>(() => CreateConfigurationFromString(configrationText));
            Assert.Equal("MaxMessageLength cannot be zero or negative.", ex.InnerException.InnerException.Message);
        }

        [Theory]
        [InlineData(0)]
        [InlineData(-1)]
        public void ShouldThrowException_WhenMaxMessageLengthSetNegativeOrZero(int maxMessageLength)
        {
            ArgumentException ex = Assert.Throws<ArgumentException>(() =>
            {
                var target = new EventLogTarget();
                target.MaxMessageLength = maxMessageLength;
            });

            Assert.Equal("MaxMessageLength cannot be zero or negative.", ex.Message);
        }


        private void AssertMessageAndLogLevelForTruncatedMessages(LogLevel loglevel, EventLogEntryType expectedEventLogEntryType, string expectedMessage, Layout entryTypeLayout)
        {
            const int expectedEntryCount = 1;
            var eventRecords = Write(loglevel, expectedEventLogEntryType, expectedMessage, entryTypeLayout, EventLogTargetOverflowAction.Truncate).ToList();

            Assert.Equal(expectedEntryCount, eventRecords.Count);
            AssertWrittenMessage(eventRecords, expectedMessage);
{'fraction_non_alphanumeric': 0.08857404021937842, 'fraction_numerical': 0.007495429616087752, 'mean_word_length': 2.4503311258278146, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7315430', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2617, 'n_words': 804}