首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nlog in Azure函数在注入DI时不起作用

Nlog in Azure函数在注入DI时不起作用
EN

Stack Overflow用户
提问于 2022-02-08 16:59:16
回答 1查看 441关注 0票数 0

参考代码摘自https://stackoverflow.com/questions/56512672/how-to-configure-nlog-for-azure-functions#:~:text=0,and%20AutoShutdown%20%3D%20false

我的StartUp.cs文件

代码语言:javascript
运行
复制
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using NLog.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

[assembly: FunctionsStartup(typeof(NLogFunctionApp.StartUp))]
namespace NLogFunctionApp
{
    public class StartUp :FunctionsStartup
    {
        private readonly NLog.Logger logger;
        public StartUp()
        {
            logger = LogManager.Setup()
                      .SetupExtensions(e=>e.AutoLoadAssemblies(false))
                        .LoadConfigurationFromFile("nlog.config", optional: false)
               .LoadConfiguration(builder => builder.LogFactory.AutoShutdown = false)
               .GetCurrentClassLogger();
        }
        public override void Configure(IFunctionsHostBuilder hostBuilder)
        {
            hostBuilder.Services.AddLogging((loggingBuilder) =>
            {
                loggingBuilder.AddNLog(new NLogProviderOptions() { ShutdownOnDispose = true });
            });
        }
    }
}

"NLogSample.cs函数文件“

代码语言:javascript
运行
复制
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using NLog;

namespace NLogFunctionApp
{
    public class NLogSample
    {
        private readonly ILogger<NLogSample> log;
        private static  Logger logCurrent;

        public NLogSample(ILogger<NLogSample> logger)
        {
            log = logger;
            logCurrent = LogManager.GetCurrentClassLogger();
        }

        [FunctionName("NLogSample")]
        public void Run([TimerTrigger("0 * * * * *")]TimerInfo myTimer)
        {
            logCurrent.Info("This is printing");
            log.LogInformation("This is not printing");
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }
    }
}

nlog.config

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="https://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="c:\temp\jjgnet-functions-internal-nlog.log"
      internalLogLevel="Debug" >

  <variable name="logDirectory" value="${currentdir}${dir-separator}logs${dir-separator}" />
  <extensions>
    <add assembly="NLog.Extensions.Logging"/>
    <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!--<target xsi:type="File" name="file-everything" fileName="${logDirectory}${shortdate}-everything.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${gdc:item=Version}|${message} ${exception:format=tostring}" />
    <target xsi:type="File" name="file-host" fileName="${logDirectory}${shortdate}-host.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${gdc:item=Version}|${message} ${exception:format=tostring}" />
    <target xsi:type="File" name="file-just-mine" fileName="${logDirectory}${shortdate}-just-mine.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${gdc:item=Version}|${message} ${exception:format=tostring}" />-->

    <target xsi:type="ApplicationInsightsTarget" name="aiTarget">
      <instrumentationKey>AzureAppInsightkeyvalue</instrumentationKey>
      <!-- Only required if not using ApplicationInsights.config -->
      <contextproperty name="threadid" layout="${threadid}" />
      <contextproperty name="AssemblyVersion" layout="${gdc:item=ExecutingAssembly-AssemblyVersion}" />
      <contextproperty name="FileVersion" layout="${gdc:item=ExecutingAssembly-FileVersion}" />
      <contextproperty name="ProductVersion" layout="${gdc:item=ExecutingAssembly-ProductVersion}" />
    </target>

    <target xsi:type="Console" name="logconsole"
            layout="${longdate}|${level}|${logger}|${message} |${all-event-properties} ${exception:format=tostring}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" minlevel="Trace" writeTo="logconsole" />
    <logger name="*" minlevel="Trace" writeTo="aiTarget" />
  </rules>
</nlog>

带有“这是打印”的代码运行良好,但是当注入DI时,它并不是打印日志。有些默认日志正在打印,但不使用log.LogInformation。

host.json是空的,那里没有默认提供的日志记录信任。

有人能帮忙吗。找了很多地方,却找不到原因。GitHub回购链接- https://github.com/jatingandhi28/NLogAzureFunctions

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-09 17:53:01

您可以尝试禁用Microsoft LoggerFactory中的筛选逻辑,方法是:

代码语言:javascript
运行
复制
new NLogProviderOptions() { ShutdownOnDispose = true }

到这个

代码语言:javascript
运行
复制
new NLogProviderOptions() { ShutdownOnDispose = true, RemoveLoggerFactoryFilter = true }

另见:https://github.com/NLog/NLog.Web/wiki/Missing-trace%5Cdebug-logs-in-ASP.NET-Core-6%3F

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71037798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档