This is an automated email from the ASF dual-hosted git repository. freeandnil pushed a commit to branch Feature/228-show-config-file-path-in-error-message in repository https://gitbox.apache.org/repos/asf/logging-log4net.git
commit 2545075d9e2d775e26eadb0bf28fec1c7ace8c3c Author: Jan Friedrich <[email protected]> AuthorDate: Thu Feb 6 17:08:41 2025 +0100 show config file path in error message fixes #227 --- src/log4net.Tests/Config/XmlConfiguratorTest.cs | 60 +++++++++++++++++++++++++ src/log4net/Config/XmlConfigurator.cs | 10 +++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/log4net.Tests/Config/XmlConfiguratorTest.cs b/src/log4net.Tests/Config/XmlConfiguratorTest.cs new file mode 100644 index 00000000..aabf3fb4 --- /dev/null +++ b/src/log4net.Tests/Config/XmlConfiguratorTest.cs @@ -0,0 +1,60 @@ +#region Apache License +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to you under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +#if NET8_0_OR_GREATER +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Xml; +using log4net.Config; +using log4net.Repository; +using log4net.Util; +using NUnit.Framework; + +namespace log4net.Tests.Config; + +/// <summary> +/// Tests for <see cref="XmlConfigurator"/> class. +/// </summary> +[TestFixture] +public class XmlConfiguratorTest +{ + /// <summary> + /// Show config file path in error message + /// </summary> + [Test] + public void ConfigureWithUnkownConfigFile() + { + Func<XmlElement?> getConfigSection = () => null; + ILoggerRepository repository = LogManager.CreateRepository(Guid.NewGuid().ToString()); + SystemInfo.EntryAssemblyLocation = Guid.NewGuid().ToString(); + List<LogLog> configurationMessages = []; + + using (new LogLog.LogReceivedAdapter(configurationMessages)) + { + typeof(XmlConfigurator) + .GetMethod("InternalConfigure", BindingFlags.NonPublic | BindingFlags.Static, [typeof(ILoggerRepository), getConfigSection.GetType()])! + .Invoke(null, [repository, getConfigSection]); + } + + Assert.That(configurationMessages, Has.Count.EqualTo(1)); + Assert.That(configurationMessages[0].Message, Contains.Substring(SystemInfo.EntryAssemblyLocation + ".config")); + } +} +#endif \ No newline at end of file diff --git a/src/log4net/Config/XmlConfigurator.cs b/src/log4net/Config/XmlConfigurator.cs index c30e5ea8..c2fd8405 100644 --- a/src/log4net/Config/XmlConfigurator.cs +++ b/src/log4net/Config/XmlConfigurator.cs @@ -66,7 +66,7 @@ public static ICollection Configure(ILoggerRepository repository) using (new LogLog.LogReceivedAdapter(configurationMessages)) { - InternalConfigure(repository); + InternalConfigure(repository, () => System.Configuration.ConfigurationManager.GetSection("log4net") as XmlElement); } repository.ConfigurationMessages = configurationMessages; @@ -74,7 +74,7 @@ public static ICollection Configure(ILoggerRepository repository) return configurationMessages; } - private static void InternalConfigure(ILoggerRepository repository) + private static void InternalConfigure(ILoggerRepository repository, Func<XmlElement?> getConfigSection) { LogLog.Debug(_declaringType, $"configuring repository [{repository.Name}] using .config file section"); @@ -90,10 +90,12 @@ private static void InternalConfigure(ILoggerRepository repository) try { - if (System.Configuration.ConfigurationManager.GetSection("log4net") is not XmlElement configElement) + if (getConfigSection() is not XmlElement configElement) { // Failed to load the xml config using configuration settings handler - LogLog.Error(_declaringType, "Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler,log4net\" />"); + LogLog.Error(_declaringType, @$"Failed to find configuration section 'log4net' in the .config file '{SystemInfo.ConfigurationFileLocation}'. +Check your .config file for the <log4net> and <configSections> elements. +The configuration section should look like: <section name=""log4net"" type=""log4net.Config.Log4NetConfigurationSectionHandler,log4net"" />"); } else {
