Skip to content

Commit 2545075

Browse files
committed
show config file path in error message
fixes #227
1 parent 4e4ca9d commit 2545075

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#region Apache License
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one or more
4+
// contributor license agreements. See the NOTICE file distributed with
5+
// this work for additional information regarding copyright ownership.
6+
// The ASF licenses this file to you under the Apache License, Version 2.0
7+
// (the "License"); you may not use this file except in compliance with
8+
// the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#endregion
19+
20+
#if NET8_0_OR_GREATER
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Reflection;
24+
using System.Xml;
25+
using log4net.Config;
26+
using log4net.Repository;
27+
using log4net.Util;
28+
using NUnit.Framework;
29+
30+
namespace log4net.Tests.Config;
31+
32+
/// <summary>
33+
/// Tests for <see cref="XmlConfigurator"/> class.
34+
/// </summary>
35+
[TestFixture]
36+
public class XmlConfiguratorTest
37+
{
38+
/// <summary>
39+
/// Show config file path in error message
40+
/// </summary>
41+
[Test]
42+
public void ConfigureWithUnkownConfigFile()
43+
{
44+
Func<XmlElement?> getConfigSection = () => null;
45+
ILoggerRepository repository = LogManager.CreateRepository(Guid.NewGuid().ToString());
46+
SystemInfo.EntryAssemblyLocation = Guid.NewGuid().ToString();
47+
List<LogLog> configurationMessages = [];
48+
49+
using (new LogLog.LogReceivedAdapter(configurationMessages))
50+
{
51+
typeof(XmlConfigurator)
52+
.GetMethod("InternalConfigure", BindingFlags.NonPublic | BindingFlags.Static, [typeof(ILoggerRepository), getConfigSection.GetType()])!
53+
.Invoke(null, [repository, getConfigSection]);
54+
}
55+
56+
Assert.That(configurationMessages, Has.Count.EqualTo(1));
57+
Assert.That(configurationMessages[0].Message, Contains.Substring(SystemInfo.EntryAssemblyLocation + ".config"));
58+
}
59+
}
60+
#endif

src/log4net/Config/XmlConfigurator.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public static ICollection Configure(ILoggerRepository repository)
6666

6767
using (new LogLog.LogReceivedAdapter(configurationMessages))
6868
{
69-
InternalConfigure(repository);
69+
InternalConfigure(repository, () => System.Configuration.ConfigurationManager.GetSection("log4net") as XmlElement);
7070
}
7171

7272
repository.ConfigurationMessages = configurationMessages;
7373

7474
return configurationMessages;
7575
}
7676

77-
private static void InternalConfigure(ILoggerRepository repository)
77+
private static void InternalConfigure(ILoggerRepository repository, Func<XmlElement?> getConfigSection)
7878
{
7979
LogLog.Debug(_declaringType, $"configuring repository [{repository.Name}] using .config file section");
8080

@@ -90,10 +90,12 @@ private static void InternalConfigure(ILoggerRepository repository)
9090

9191
try
9292
{
93-
if (System.Configuration.ConfigurationManager.GetSection("log4net") is not XmlElement configElement)
93+
if (getConfigSection() is not XmlElement configElement)
9494
{
9595
// Failed to load the xml config using configuration settings handler
96-
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\" />");
96+
LogLog.Error(_declaringType, @$"Failed to find configuration section 'log4net' in the .config file '{SystemInfo.ConfigurationFileLocation}'.
97+
Check your .config file for the <log4net> and <configSections> elements.
98+
The configuration section should look like: <section name=""log4net"" type=""log4net.Config.Log4NetConfigurationSectionHandler,log4net"" />");
9799
}
98100
else
99101
{

0 commit comments

Comments
 (0)