c# - 在 C# 中使用 log4net 附加到文件
问题描述
我是 log4net 的新手,想创建一个简单的控制台应用程序,它执行以下操作:
读取一个数字作为输入
循环很多次迭代
- 打印运行循环所花费的时间
- 创建以下格式的日志文件:
MM/DD/YYYY
- 输入:<num>
- 时间:<time>
毫秒
在我的 AssemblyInfo.cs 中,我添加了以下行:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
我的 App.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="MyLog.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%dateExtra Info: %property{input}%property{time}%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
我的代码如下所示:
using log4net;
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
private static long num;
private static readonly ILog log = LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
Console.Write("Please enter a number: ");
try
{
// gets number as input
num = long.Parse(Console.ReadLine());
if (num <= 0) throw new Exception("Number must be positive and non zero");
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
// create new stopwatch instance
Stopwatch sw = new Stopwatch();
// starts the timer
sw.Start();
// runs loop [num] times
for (long i = 0; i < num; i++) ;
// stops the timer
sw.Stop();
Console.WriteLine("Loop ran for {0}ms", sw.ElapsedMilliseconds);
GlobalContext.Properties["input"] = " - Input : " + num;
GlobalContext.Properties["time"] = " - Time : " + sw.ElapsedMilliseconds + "ms";
Console.Read();
}
}
}
该文件确实被创建,但它被创建为空。这里可能是什么问题?
解决方案
您需要使用以下命令记录一些内容
log.Info(string.Format("Input: {0} - Time: {1}ms", input, time));
这就像
using log4net;
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
private static long num;
private static readonly ILog log = LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
...
Console.WriteLine("Loop ran for {0}ms", sw.ElapsedMilliseconds);
log.Info(string.Format("Input: {0} - Time: {1}ms", num, sw.ElapsedMilliseconds));
...
}
}
}
推荐阅读
- sorting - 你如何分析这个排序算法的时间复杂度?
- react-native - VirtualizedLists 永远不应该嵌套错误
- javascript - 尽管处理它但在控制台上出现错误(MERN + Redux)
- python - 如何将情感分析模型应用于python中的1个scentence?
- javascript - 如何使这些输入可编辑但将它们设置为默认值 - 反应?
- r - 在R中编织到PDF时找不到参考
- javascript - React Native - 将 ActivityIndicator 重新定位到登录按钮
- javascript - addEventListener 不适用于 document.createElement 元素
- flutter - 如何在 List.map() 函数中等待未来并返回字符串列表
- python - 即使我正在*制作*一个文件,我也会收到“FileNotFoundError”