【问题标题】:Logger writes nothing to Azure Function 2.0记录器不会向 Azure Function 2.0 写入任何内容
【发布时间】:2018-08-01 11:15:08
【问题描述】:

我正在尝试在 Azure 函数中使用除标准 Tracewriter 之外的另一个记录器。大多数 NLog 在运行 Azure 函数时都在本地工作,但在我部署到 Azure 后什么都没有写入。

通过 blob 加载配置。我将配置文件存储在存储 blob 中并将其作为参数加载

[FunctionName("Update")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = null)]HttpRequest req, 
        [Blob("nlog/nlog.config", FileAccess.Read)] string inputBlob)

Logger.LoadConfiguration(inputBlob);
Logger.Info("This does not show in Azure");

...

我加载配置的记录器类

public class Logger
    {
        public static Logger Log = LogManager.GetCurrentClassLogger();

        private static bool _isConfigrued;

        public static void LoadConfiguration(string xmlString)
        {
            if (string.IsNullOrEmpty(xmlString))
                return;

            if (!_isConfigrued)
            {
                StringReader sr = new StringReader(xmlString);
                XmlReader xr = XmlReader.Create(sr);
                LogManager.Configuration = new XmlLoggingConfiguration(xr, null);

                Log = LogManager.GetCurrentClassLogger();
                _isConfigrued = true;
            }
        }
    }

然后我将 NLog 配置为写入跟踪

  <targets>
    <target name='console' type='Console' layout='Console ${message}' />
    <target name="trace" xsi:type="Trace" layout="Trace ${message}" />
  </targets>

 <rules>
    <logger name="*" minlevel="Trace" writeTo="trace" />
    <logger name="*" minlevel="Trace" writeTo="console" /> 
  </rules>

但是没有任何东西被写入跟踪/控制台

我也尝试加载此处建议的配置

https://blogs.msdn.microsoft.com/waws/2017/03/16/nlog-and-database/

也不行

我还尝试了Microsoft.Extentions.Loggin.Ilogger,在本地工作,但在 Azure 上没有打印任何内容。

不会抛出异常,只是清空控制台窗口。

【问题讨论】:

  • 如果您在app.config 中配置它,这将不起作用,因为函数不使用该文件。
  • 我从 blob 存储或文件系统和链接中加载配置文件
  • 您在哪里检查您的日志?在 Functions 门户中,您看到的日志是从文件系统流式传输的日志,而不是来自控制台日志。
  • 我在这里读到了目标 xsi:type"Trace" 将进入流媒体。就像原来的tracewriter一样
  • 我可以确认,对于 v2,使用 TraceWriter 不会向 Azure 门户中的控制台输出任何内容。

标签: c# .net-core azure-functions nlog


【解决方案1】:

在 Azure 版本上运行时。 1 然后可以将 NLog 输出转发到 TextWriter 记录器:

在 Azure 版本上运行时。 2 然后可以将 NLog 输出转发到 Microsoft ILogger:

另见:https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-cloud-logging-with-Azure-function-or-AWS-lambda

另见:https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library

如果需要部署 nlog.config,则可能必须将其包含在 Azure 功能的“应用程序设置”中。另见https://stackoverflow.com/a/47332193

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2013-11-24
    • 1970-01-01
    • 2021-02-04
    相关资源
    最近更新 更多