log4net:ERROR 读取 ConfigurationSettings 时出现异常。检查您的 .config 文件格式是否正确 XML
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML
我正在尝试将 log4net 实施到我的控制台应用程序中,但出现了一堆错误,例如
log4net: Error exception while reading configurationSettings.
我用谷歌搜索,在检查了 Whosebug 上提出的许多问题后,我发布了这个问题。我知道我的 app.config 文件没有问题。
这是我的 App.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
<root>
<level value="INFO"/>
<appender-ref ref="ConsoleAppender"/>
</root>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
我的日志助手Class:
public class LogHelper
{
public static log4net.ILog GetLogger([CallerFilePath]string filename = "")
{
return log4net.LogManager.GetLogger(filename);
}
}
这就是我在主程序中使用它的方式 class Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Shopping_List
{
class Program
{
private static readonly log4net.ILog log = LogHelper.GetLogger(); //log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static void Main()
{
log.Info("This is info message");
MyClass newClass = new MyClass();
newClass.MyMethod();
log.Info("This is info message");
Console.ReadKey();
}
}
}
这是我的另一个 class
public class MyClass
{
//var itemList = new List<string>();
List<String> itemList = new List<string>();
int choices;
public void MyMethod()
{
//var itemList = new List<string>();
//int choices;
while (true)
{
try
{
Console.WriteLine("Shopping list:\n1. Add to list\n2. Remove from list\n3. List the items on the shopping list\n4. Exit");
choices = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("\n ");
Console.WriteLine("You have Entered non-numeric value, you can only choose between 1 to 4");
Console.WriteLine("\n ");
}
Console.WriteLine("\n ");
if (choices == 1)
{
itemAdded();
}
else if (choices == 2)
{
itemRemoved();
}
else if (choices == 3)
{
readList();
}
else if (choices == 4)
{
//break;
Environment.Exit(0);
}
else
{
Console.WriteLine("please enter valid choice!!!");
}
}
}
public void itemAdded()
{
//var itemList = new List<string>();
Console.WriteLine("\nWhat would you like to add in the list?");
string itemAdded = Console.ReadLine();
if (string.IsNullOrEmpty(itemAdded))
{
Console.WriteLine("you add nothing!!!");
}
else
{
itemList.Add(itemAdded);
Console.WriteLine(itemAdded + " is added to the list");
}
}
public void itemRemoved()
{
//var itemList = new List<string>();
Console.WriteLine("\nWhat would you like to remove from the list?");
string itemToRemove = Console.ReadLine();
if (string.IsNullOrEmpty(itemToRemove))
{
Console.WriteLine("you removed nothing");
}
else
{
bool isMatch = false;
foreach (string item in itemList)
{
if (item == itemToRemove)
{
isMatch = true;
}
}
if (isMatch)//if ismatch is true
{
itemList.Remove(itemToRemove);
Console.WriteLine(itemToRemove + " is successfully removed from your list");
}
else
{
Console.WriteLine("Item is not in the list");
}
}
}
public void readList()
{
//var itemList = new List<string>();
foreach (string item in itemList)
{
Console.WriteLine(item);
}
}
}
我没有在 Class.
中使用 log4net
您的 App.config 文件 log4net
部分配置错误。您过早关闭了 log4net
节点。
应该是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="ConsoleAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
我正在尝试将 log4net 实施到我的控制台应用程序中,但出现了一堆错误,例如
log4net: Error exception while reading configurationSettings.
我用谷歌搜索,在检查了 Whosebug 上提出的许多问题后,我发布了这个问题。我知道我的 app.config 文件没有问题。
这是我的 App.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
<root>
<level value="INFO"/>
<appender-ref ref="ConsoleAppender"/>
</root>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
我的日志助手Class:
public class LogHelper
{
public static log4net.ILog GetLogger([CallerFilePath]string filename = "")
{
return log4net.LogManager.GetLogger(filename);
}
}
这就是我在主程序中使用它的方式 class Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Shopping_List
{
class Program
{
private static readonly log4net.ILog log = LogHelper.GetLogger(); //log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static void Main()
{
log.Info("This is info message");
MyClass newClass = new MyClass();
newClass.MyMethod();
log.Info("This is info message");
Console.ReadKey();
}
}
}
这是我的另一个 class
public class MyClass
{
//var itemList = new List<string>();
List<String> itemList = new List<string>();
int choices;
public void MyMethod()
{
//var itemList = new List<string>();
//int choices;
while (true)
{
try
{
Console.WriteLine("Shopping list:\n1. Add to list\n2. Remove from list\n3. List the items on the shopping list\n4. Exit");
choices = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("\n ");
Console.WriteLine("You have Entered non-numeric value, you can only choose between 1 to 4");
Console.WriteLine("\n ");
}
Console.WriteLine("\n ");
if (choices == 1)
{
itemAdded();
}
else if (choices == 2)
{
itemRemoved();
}
else if (choices == 3)
{
readList();
}
else if (choices == 4)
{
//break;
Environment.Exit(0);
}
else
{
Console.WriteLine("please enter valid choice!!!");
}
}
}
public void itemAdded()
{
//var itemList = new List<string>();
Console.WriteLine("\nWhat would you like to add in the list?");
string itemAdded = Console.ReadLine();
if (string.IsNullOrEmpty(itemAdded))
{
Console.WriteLine("you add nothing!!!");
}
else
{
itemList.Add(itemAdded);
Console.WriteLine(itemAdded + " is added to the list");
}
}
public void itemRemoved()
{
//var itemList = new List<string>();
Console.WriteLine("\nWhat would you like to remove from the list?");
string itemToRemove = Console.ReadLine();
if (string.IsNullOrEmpty(itemToRemove))
{
Console.WriteLine("you removed nothing");
}
else
{
bool isMatch = false;
foreach (string item in itemList)
{
if (item == itemToRemove)
{
isMatch = true;
}
}
if (isMatch)//if ismatch is true
{
itemList.Remove(itemToRemove);
Console.WriteLine(itemToRemove + " is successfully removed from your list");
}
else
{
Console.WriteLine("Item is not in the list");
}
}
}
public void readList()
{
//var itemList = new List<string>();
foreach (string item in itemList)
{
Console.WriteLine(item);
}
}
}
我没有在 Class.
中使用 log4net您的 App.config 文件 log4net
部分配置错误。您过早关闭了 log4net
节点。
应该是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="ConsoleAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>