<commit_before>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.IO;
using Xunit;

namespace System.Diagnostics.Tests
{
    public class EventLogTests : FileCleanupTestBase
    {
        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void EventLogReinitializationException()
        {
            using (EventLog eventLog = new EventLog())
            {
                eventLog.BeginInit();
                Assert.Throws<InvalidOperationException>(() => eventLog.BeginInit());
                eventLog.EndInit();
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
        public void ClearLog()
        {
            string log = "ClearTest";
            string source = "Source_" + nameof(ClearLog);

            try
            {
                EventLog.CreateEventSource(source, log);
                using (EventLog eventLog = new EventLog())
                {
                    eventLog.Source = source;
                    Helpers.RetryOnWin7(() => eventLog.Clear());
                    Assert.Equal(0, Helpers.RetryOnWin7((() => eventLog.Entries.Count)));
                    Helpers.RetryOnWin7(() => eventLog.WriteEntry("Writing to event log."));
                    Helpers.WaitForEventLog(eventLog, 1);
                    Assert.Equal(1, Helpers.RetryOnWin7((() => eventLog.Entries.Count)));
                }
            }
            finally
            {
                EventLog.DeleteEventSource(source);
                Helpers.RetryOnWin7(() => EventLog.Delete(log));
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void ApplicationEventLog_Count()
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                Assert.InRange(Helpers.RetryOnWin7((() => eventLog.Entries.Count)), 1, int.MaxValue);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
        public void DeleteLog()
        {
            string log = "DeleteTest";
            string source = "Source_" + nameof(DeleteLog);

            try
            {
                EventLog.CreateEventSource(source, log);
                Assert.True(EventLog.Exists(log));
            }
            finally
            {
                EventLog.DeleteEventSource(source);
                Helpers.RetryOnWin7(() => EventLog.Delete(log));
                Assert.False(EventLog.Exists(log));
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void CheckLogName_Get()
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                Assert.False(string.IsNullOrEmpty(eventLog.LogDisplayName));
                if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "en" )
                    Assert.Equal("Application", eventLog.LogDisplayName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void CheckMachineName_Get()
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                Assert.Equal(".", eventLog.MachineName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void GetLogDisplayName_NotSet_Throws()
        {
            using (EventLog eventLog = new EventLog())
            {
                eventLog.Log = Guid.NewGuid().ToString("N");
                Assert.Throws<InvalidOperationException>(() => eventLog.LogDisplayName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void GetLogDisplayName_Set()
        {
            using (EventLog eventLog = new EventLog())
            {
                eventLog.Log = "Application";

                Assert.False(string.IsNullOrEmpty(eventLog.LogDisplayName));
                if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "en" )
                    Assert.Equal("Application", eventLog.LogDisplayName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void EventLogs_Get()
        {
            Assert.Throws<ArgumentException>(() => EventLog.GetEventLogs(""));
            EventLog[] eventLogCollection = EventLog.GetEventLogs();
            Assert.Contains(eventLogCollection, eventlog => eventlog.Log.Equals("Application"));
            Assert.Contains(eventLogCollection, eventlog => eventlog.Log.Equals("Security"));
            Assert.Contains(eventLogCollection, eventlog => eventlog.Log.Equals("System"));
        }<commit_msg>Fix EventLog tests on latest Win10 (#39182)<commit_after>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.IO;
using Xunit;

namespace System.Diagnostics.Tests
{
    public class EventLogTests : FileCleanupTestBase
    {
        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void EventLogReinitializationException()
        {
            using (EventLog eventLog = new EventLog())
            {
                eventLog.BeginInit();
                Assert.Throws<InvalidOperationException>(() => eventLog.BeginInit());
                eventLog.EndInit();
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
        public void ClearLog()
        {
            string log = "ClearTest";
            string source = "Source_" + nameof(ClearLog);

            try
            {
                EventLog.CreateEventSource(source, log);
                using (EventLog eventLog = new EventLog())
                {
                    eventLog.Source = source;
                    Helpers.RetryOnWin7(() => eventLog.Clear());
                    Assert.Equal(0, Helpers.RetryOnWin7((() => eventLog.Entries.Count)));
                    Helpers.RetryOnWin7(() => eventLog.WriteEntry("Writing to event log."));
                    Helpers.WaitForEventLog(eventLog, 1);
                    Assert.Equal(1, Helpers.RetryOnWin7((() => eventLog.Entries.Count)));
                }
            }
            finally
            {
                EventLog.DeleteEventSource(source);
                Helpers.RetryOnWin7(() => EventLog.Delete(log));
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void ApplicationEventLog_Count()
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                Assert.InRange(Helpers.RetryOnWin7((() => eventLog.Entries.Count)), 1, int.MaxValue);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
        public void DeleteLog()
        {
            string log = "DeleteTest";
            string source = "Source_" + nameof(DeleteLog);

            try
            {
                EventLog.CreateEventSource(source, log);
                Assert.True(EventLog.Exists(log));
            }
            finally
            {
                EventLog.DeleteEventSource(source);
                Helpers.RetryOnWin7(() => EventLog.Delete(log));
                Assert.False(EventLog.Exists(log));
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void CheckLogName_Get()
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                Assert.False(string.IsNullOrEmpty(eventLog.LogDisplayName));
                if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "en" )
                    Assert.Equal("Application", eventLog.LogDisplayName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void CheckMachineName_Get()
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                Assert.Equal(".", eventLog.MachineName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void GetLogDisplayName_NotSet_Throws()
        {
            using (EventLog eventLog = new EventLog())
            {
                eventLog.Log = Guid.NewGuid().ToString("N");
                Assert.Throws<InvalidOperationException>(() => eventLog.LogDisplayName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void GetLogDisplayName_Set()
        {
            using (EventLog eventLog = new EventLog())
            {
                eventLog.Log = "Application";

                Assert.False(string.IsNullOrEmpty(eventLog.LogDisplayName));
                if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "en" )
                    Assert.Equal("Application", eventLog.LogDisplayName);
            }
        }

        [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
        public void EventLogs_Get()
        {
            Assert.Throws<ArgumentException>(() => EventLog.GetEventLogs(""));
            EventLog[] eventLogCollection = EventLog.GetEventLogs();
            Assert.Contains(eventLogCollection, eventlog => eventlog.Log.Equals("Application"));
            Assert.Contains(eventLogCollection, eventlog => eventlog.Log.Equals("System"));
        }
{'fraction_non_alphanumeric': 0.11073013311819283, 'fraction_numerical': 0.003327954820492134, 'mean_word_length': 1.9322885866351271, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 34, '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': '418974', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2543, 'n_words': 433}