【发布时间】:2017-09-14 10:09:15
【问题描述】:
我在 Azure 中创建了一个 WebJob。我使用了 Nlog 并将日志文件指向 WebJob 的 exe 文件的“基本目录”。 Web 作业已创建,并且运行成功,没有任何错误。此外,Nlog 正在我的本地系统中创建日志文件。但是在天蓝色中,我找不到日志文件。我进入“app_data\jobs\triggered”文件夹,找到了我创建的工作。但是没有创建日志文件。我能够在 azure 中编写和查看控制台日志。
编辑
程序.cs
using NLog;
using System;
namespace FacilitationPortal.WebJob {
class Program
{
static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
string nlogServicePath = AppDomain.CurrentDomain.BaseDirectory + "NLog-WebSchedulerService.config";
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(nlogServicePath, true);
logger.Debug("Initializing the Web Scheduler Service");
Console.WriteLine("Initializing the Web Scheduler Service");
StartServices();
}
private static void StartServices()
{
logger.Debug("INSIDE - START SERVICES");
}
}
}
NLog-WebSchedulerService.config 文件
<?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" autoReload="true">
<targets >
<target xsi:type="File" name="logfile"
fileName="${basedir}/logs/WebScheduler.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${gdc:item=hostname} ${gdc:item=useremail} (${logger} - ${mdc:item=actionname}) ${message} ${exception:format=tostring}"
archiveEvery="Day"
archiveFileName ="${basedir}/logs/WebScheduler.${date:format=yyyy-MM-dd HH.mm}.{#}.zip"
archiveNumbering ="Sequence"
maxArchiveFiles="30"
fileAttributes="Compressed"
enableArchiveFileCompression = "true">
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile">
</logger>
</rules>
</nlog>
Console.WriteLine() 正在 azure 控制台中打印日志。但是 logger.Debug() 不会在 exe 所在的位置创建日志文件。如果我在本地系统中运行相同的应用程序,则会创建日志文件。
谢谢
【问题讨论】:
-
能否贴出相关代码?
-
我把
${basedir}替换成已有的物理目录,比如fileName="D:\home\data\logs\WebScheduler.log",发现日志是在Azure里创建的,你可以试试。
标签: c# nlog azure-webjobs