Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show config file path in error message #228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/log4net.Tests/Config/XmlConfiguratorTest.cs
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions src/log4net/Config/XmlConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ 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;

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");

Expand All @@ -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
{
Expand Down