Azure Function App 依赖注入的日志记录器未记录

huangapple go评论50阅读模式
英文:

Azure Function App Dependency Inject Logger not logging

问题

以下是翻译的部分:

"我有一个函数应用程序,我尝试在一个类中使用依赖注入(DI)来为记录器(ILogger)提供服务,这样我就不必将ILogger传递给每个函数。我已经进行了一些研究,发现我应该在类的构造函数中使用ILogger。我已经做了所有我认为是正确的更改,但仍然无法从该类中记录任何内容。

我已经将日志记录添加到我的Startup类中,更新了hosts.json文件,并在构造函数中使用了ILogger。没有错误,但没有任何日志记录。有人看到我漏掉了什么吗?

Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddLogging();
    builder.Services.AddTransient<IDataManager, DataManager>();
}

Host.json

{
    "version": "2.0",
    "logging": {
        "fileLoggingMode": "always",
        "logLevel": {
            "default": "Debug"
        },
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": false,
                "excludedTypes": "Request"
            }
        }
    }
}

DataManager.cs

public class DataManager : IDataManager
{
    private DataContext _db;
    private readonly ILogger<DataManager> _log;

    public DataManager(ILogger<DataManager> log, DataContext dataContext)
    {
        _db = dataContext;
        _log = log;
    }
}

希望这对你有所帮助。如果你需要更多信息,请随时提出。

英文:

I have a Function App that I’m trying to using DI in a class for the logger so that I don’t have to pass the ILogger to every function. I’ve been doing some researching and found where I’m supposed to use ILogger in the class constructor. I’ve done all of the changes I believe are correct but it’s still not logging anything from that class.

I’ve added the logging to my Startup class, updated the hosts.json file, and used ILogger in the constructor. There are no errors but nothing gets logged. Does anyone see something I’m missing?

Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddLogging();
            builder.Services.AddTransient&lt;IDataManager, DataManager&gt;();
        }

Host.json

{
  &quot;version&quot;: &quot;2.0&quot;,
  &quot;logging&quot;: {
    &quot;fileLoggingMode&quot;: &quot;always&quot;,
    &quot;logLevel&quot;: {
      &quot;default&quot;: &quot;Debug&quot;
    },
    &quot;applicationInsights&quot;: {
      &quot;samplingSettings&quot;: {
        &quot;isEnabled&quot;: false,
        &quot;excludedTypes&quot;: &quot;Request&quot;
      }
    }
  }
}

DataManager.cs

public class DataManager : IDataManager
    {
        private DataContext _db;
        private readonly ILogger&lt;DataManager&gt; _log;

        public DataManager(ILogger&lt;DataManager&gt; log, DataContext dataContext)
        {
            _db = dataContext;
            _log = log;
        }
}

答案1

得分: 1

我在我的环境中进行了复制,并获得了如下所示的预期结果,代码来自@nareshnagpal06的GitHub存储库:

Startup.cs:

using Microsoft.Azure.Functions.Extensions.DependencyInjection; // 安装 NuGet 包 - "Microsoft.Azure.Functions.Extensions"
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

[assembly: FunctionsStartup(typeof(FunctionApp50.Startup))]

namespace FunctionApp50
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<IHelperClass, HelperClass>();
        }
    }
}

HelperClass.cs:

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionApp50
{
    public class HelperClass : IHelperClass
    {
        private static ILogger<IHelperClass> _logger;
        public HelperClass(ILogger<IHelperClass> logger)
        {
            _logger = logger;
        }
        public void Dowork()
        {
            _logger.LogInformation("Dowork: Execution Started");
            /* 其余功能如下
               ...
               ...
            */
            _logger.LogInformation("Dowork: Execution Completed");
        }
    }
}

IHelperClass.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionApp50
{
    public interface IHelperClass
    {
        void Dowork();
    }
}

Function1.cs:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp50
{
    public class Function1 // 确保类不是静态的(默认情况下)
    {
        private IHelperClass _helper;

        public Function1(IHelperClass helper)
        {
            _helper = helper;
        }

        [FunctionName("Function1")]
        public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            // 调用帮助程序
            _helper.Dowork();
        }
    }
}

host.json:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    },
    "logLevel": {
      "Function": "Information"
    }
  }
}

终端输出:

Azure Function App 依赖注入的日志记录器未记录

文件输出:

首先运行,然后粘贴%temp%\LogFiles\Application\Functions.,然后点击位置中的主机文件,然后点击日志文件,输出如下:

Azure Function App 依赖注入的日志记录器未记录

英文:

I have reproduced in my environment and got expected results as below and the code taken from @nareshnagpal06 's GitHub repository:

Startup.cs:

using Microsoft.Azure.Functions.Extensions.DependencyInjection; // install nuget - &quot;Microsoft.Azure.Functions.Extensions&quot;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

[assembly: FunctionsStartup(typeof(FunctionApp50.Startup))]

namespace FunctionApp50
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton&lt;IHelperClass, HelperClass&gt;();

        }
    }
}

HelperClass.cs:

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionApp50
{
    public class HelperClass : IHelperClass
    {
        private static ILogger&lt;IHelperClass&gt; _logger;
        public HelperClass(ILogger&lt;IHelperClass&gt; logger)
        {

            _logger = logger;
        }
        public void Dowork()
        {
            _logger.LogInformation(&quot;Dowork: Execution Started&quot;);
            /* rest of the functionality below
                .....
                .....
            */
            _logger.LogInformation(&quot;Dowork: Execution Completed&quot;);
        }
    }
}

IHelperClass.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionApp50
{
    public interface IHelperClass
    {
        void Dowork();
    }
}

Function1.cs:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp50
{
    public class Function1 // Ensure class is not static (which comes by default)
    {
        private IHelperClass _helper;

        public Function1(IHelperClass helper)
        {
            _helper = helper;
        }

        [FunctionName(&quot;Function1&quot;)]
        public void Run([TimerTrigger(&quot;0 */1 * * * *&quot;)] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($&quot;C# Timer trigger function executed at: {DateTime.Now}&quot;);
            // call helper 
            _helper.Dowork();
        }
    }
}

host.json:

{
  &quot;version&quot;: &quot;2.0&quot;,
  &quot;logging&quot;: {
    &quot;fileLoggingMode&quot;: &quot;always&quot;,
    &quot;applicationInsights&quot;: {
      &quot;samplingExcludedTypes&quot;: &quot;Request&quot;,
      &quot;samplingSettings&quot;: {
        &quot;isEnabled&quot;: true
      }
    },
    &quot;logLevel&quot;: {
      &quot;Function&quot;: &quot;Information&quot;
    }
  }
}

Terminal Output:

Azure Function App 依赖注入的日志记录器未记录

File Output:

Firstly go to run, then paste %temp%\LogFiles\Application\Functions.,then click on Host file in the location then click on log file then the output is:

Azure Function App 依赖注入的日志记录器未记录

huangapple
  • 本文由 发表于 2023年2月14日 05:42:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定