From 2545075d9e2d775e26eadb0bf28fec1c7ace8c3c Mon Sep 17 00:00:00 2001 From: Jan Friedrich Date: Thu, 6 Feb 2025 17:08:41 +0100 Subject: [PATCH] show config file path in error message fixes #227 --- .../Config/XmlConfiguratorTest.cs | 60 +++++++++++++++++++ src/log4net/Config/XmlConfigurator.cs | 10 ++-- 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/log4net.Tests/Config/XmlConfiguratorTest.cs 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; + +/// +/// Tests for class. +/// +[TestFixture] +public class XmlConfiguratorTest +{ + /// + /// Show config file path in error message + /// + [Test] + public void ConfigureWithUnkownConfigFile() + { + Func getConfigSection = () => null; + ILoggerRepository repository = LogManager.CreateRepository(Guid.NewGuid().ToString()); + SystemInfo.EntryAssemblyLocation = Guid.NewGuid().ToString(); + List 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 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 and elements. The configuration section should look like:
"); + LogLog.Error(_declaringType, @$"Failed to find configuration section 'log4net' in the .config file '{SystemInfo.ConfigurationFileLocation}'. +Check your .config file for the and elements. +The configuration section should look like:
"); } else {