c# - 在外部库中使用 TraceWriter 进行 Azure 函数日志记录

标签 c# logging azure-webjobs azure-functions

如何重用 Azure 函数中可用的 TraceWriter 对象来记录外部引用库中的信息?我尝试使用构造函数传入对象并引用 TraceWriter 类 (web.http.tracing)。我没有运气,因为类(class)似乎不同。

最佳答案

精简版 使用 this nuget package 中提供的 Microsoft.Azure.WebJobs.Host.TraceWriter .

或者,将您的函数构建为 Web 项目,然后您可以在本地进行调试。 You can find a sample here .

长版

这里的问题是您使用了错误的 TraceWriter。

我在 Azure 函数中使用了 Azure 函数记录器来输出记录器的类型。

log.Info(log.GetType().ToString());

给出了以下内容:

Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter

我也期待一个 Web/Http TraceWriter 并且很惊讶还有另一个实现要处理。微软真的可以创建一个标准方法,或者至少为我们提供一个漂亮干净的错误、警告、信息、详细等界面。也许是 .Net Standard 的东西……拜托。

我将创建自己的界面并包装我的应用记录器和 Azure 记录器,这样我就可以注入(inject)我需要的任何一个,而不会在我的代码中进一步引起麻烦。这也将提供一些保护,免受 future 重大变化造成的潜在痛苦。

无论如何,我跑题了,然后我跟踪 Microsoft.Azure.WebJobs.Script.InterceptingTraceWriterAzure Functions / Webjobs scripting GitHub repo然后是 Nuget 包。我已经对此进行了测试,将 Azure Function 记录器传递到您的外部程序集并继续从那里登录到 Azure Function 环境时工作正常。

这是一个例子:

using Microsoft.Azure.WebJobs.Host;

public static void TryLog(TraceWriter azureFunctionsLogger)
{
    azureFunctionsLogger.Info("************** IT WORKED **************");
}

我喜欢 Azure 函数的潜力,但它仍然有点不成熟且过于复杂。

希望对您有所帮助。

添加了一个非常简单的单类记录器来说明。

它写入 Azure Functions Logger 或标准 Systems.Diagnostics.Trace。您需要将其粘贴到标准 C# 控制台应用程序的 Program.cs 的内容上。您还需要包含 Nuget 包 Microsoft.Azure.WebJobs .

namespace LoggingTestConsole
{
    using System;

    /// <summary>
    /// Generic logging interface for portability 
    /// </summary>
    public interface ILogger
    {
        void Error(string message);
        void Information(string message);
        void Warning(string message);
    }


    /// <summary>
    /// Azure Functions logger
    /// </summary>
    public class AzureFunctionLogger : ILogger
    {
        private static Microsoft.Azure.WebJobs.Host.TraceWriter _logger;

        public AzureFunctionLogger(Microsoft.Azure.WebJobs.Host.TraceWriter logger)
        {
            _logger = logger;
        }

        public void Error(string message)
        {
            _logger.Error(message);
        }

        public void Information(string message)
        {
            _logger.Info(message);
        }

        public void Warning(string message)
        {
            _logger.Warning(message);
        }
    }


    /// <summary>
    /// Windows Trace logger
    /// </summary>
    public class TraceLogger : ILogger
    {
        public TraceLogger()
        {
            System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Console.Out));
        }

        public void Error(string message)
        {
            System.Diagnostics.Trace.TraceError(message);
        }


        public void Information(string message)
        {
            System.Diagnostics.Trace.TraceInformation(message);
        }

        public void Warning(string message)
        {
            System.Diagnostics.Trace.TraceWarning(message);
        }

        public void Warning(string format, params object[] args)
        {
            System.Diagnostics.Trace.TraceWarning(format, args);
        }
    }

    /// <summary>
    /// You would put this in a separate project and just share the ILogger interface.
    /// Pass the relevant logger in from Azure Functions or a standard windows Trace logger.
    /// </summary>
    public class DoStuff
    {
        public DoStuff(ILogger logger)
        {
            logger.Information("We are logging to logger you passed in!");
        }
    }

    public class Program
    {

        /// <summary>
        /// Sample usage
        /// </summary>
        static void Main(string[] args)
        {
            // var loggerEnvironment = "AzureFunctions";
            var loggerEnvironment = "ConsoleApp";

            ILogger logger = null;

            if (loggerEnvironment == "AzureFunctions")
            {
                Microsoft.Azure.WebJobs.Host.TraceWriter azureFunctionLogger = null;
                logger = new AzureFunctionLogger(azureFunctionLogger);
            }
            else if (loggerEnvironment == "ConsoleApp")
            {
                logger = new TraceLogger();
            }

            var doStuff = new DoStuff(logger);
            Console.ReadKey();
        }
    }
}

关于c# - 在外部库中使用 TraceWriter 进行 Azure 函数日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391185/

相关文章:

c# - C#中如何调用构造函数

c# - 处理警告的最佳实践

c# - ASP.NET 站点在网络服务器上抛出编译错误

Java 登录到 2 个不同的文件

azure - 403(禁止)从 Visual Studio 发布 Azure Web 作业

c# - 如何跟踪调用方法的次数

logging - Logstash 索引器是否可扩展?

sql-server - 限制 NLog 数据库目标大小

azure - .NET Core 是否支持创建由 Azure QueueTrigger 启动的 Web 作业?

php - 如何在 Azure 上的 PHP webjob 中压缩文件?