我试着在LINQPad下测试LINQPad。
我成功地链接了它,我的代码编译得很好。但是,NLog没有编写日志文件,因为它没有配置。
我试着制作各种配置文件,比如:NLog.config和LINQPad.config,但是看起来我做得不对。
我在LINQPad下的测试代码是:
void Main()
{
try
{
int zero = 0;
int result = 5 / zero;
}
catch (DivideByZeroException ex)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.ErrorException("Whoops!", ex);
}
}配置代码:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="logfile.log" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>配置文件放在哪里?
发布于 2014-06-03 21:02:25
感谢@pasty和@nemesv的评论,这个问题已经解决了。
配置文件必须命名为NLog.config,并且必须放在LINQPad.exe文件夹中。
日志文件(在我的例子中是logfile.log)出现在同一个文件夹中。
为了编写日志文件,LINQPad需要对文件夹进行写访问。
LINQPad 必须在设置配置文件后重新启动。
编辑
如果启动LINQPad、加载或编写代码并运行它,则日志文件将显示在LINQPad.exe文件夹中。
如果通过单击保存的代码文件(如我的示例中的NLog Test.linq )来运行代码,则日志文件将出现在代码文件的文件夹中。(在我的例子中是C:\Users\Miroslav\Documents\LINQPad Queries)
发布于 2014-07-08 13:42:31
您可以使用LinqPad的扩展来启用日志记录:您将放置到My Extensions脚本的所有内容都将对其他脚本可用。
向My Extensions添加一个具有以下内容的类:
public static class NLogLinqPadExtensions
{
public static void ConfigureLogging()
{
var nlogConfig = @"
<nlog>
<targets>
<target name='console' type='Console' layout='${date:format=dd/MM/yy HH\:mm\:ss\:fff} | ${level:uppercase=true} | ${message} | ${exception:format=tostring}' />
</targets>
<rules>
<logger name='*' minlevel='Debug' writeTo='console' />
</rules>
</nlog>
";
using (var sr = new StringReader(nlogConfig))
{
using (var xr = XmlReader.Create(sr))
{
NLog.LogManager.Configuration = new XmlLoggingConfiguration(xr, null);
NLog.LogManager.ReconfigExistingLoggers();
}
}
}
}不要忘记将NLog包添加到这个脚本中。
此示例配置控制台记录器--您将在LinqPad的结果窗口中看到日志消息,这对于长期运行的进程和实用程序脚本来说非常酷。
要启用日志记录,您必须从脚本中调用这个扩展。创建一个新的LinqPad脚本并尝试如下:
NLogLinqPadExtensions.ConfigureLogging();
var logger = LogManager.GetLogger("Foo");
logger.Debug("Hi");发布于 2016-09-20 16:08:22
作为Valeriu Caraulean's answer中提供的解决方案的一个次要替代方案,您可以添加如下方法来配置NLog,但可以使用现有的配置文件:
public static void ConfigureNLog()
{
LogManager.Configuration = new XmlLoggingConfiguration(@"C:\path\to\existing\config\file\NLog.xml");
LogManager.ReconfigExistingLoggers();
}您需要将以下内容添加到LINQPad查询的命名空间导入:
NLog
NLog.Confighttps://stackoverflow.com/questions/24024336
复制相似问题