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

c# – Azure函数在外部库中使用TraceWriter进行日志记录,第1张

概述如何重用天文函数中可用的TraceWriter对象来记录外部引用库中的信息?我尝试使用构造函数传递对象并引用TraceWriter类(web.http.tracing).我没有运气,因为班级似乎不一样. 短版 使用 this nuget package中提供的Microsoft.Azure.WebJobs.Host.TraceWriter. 或者,将您的功能构建为Web项目,您可以在本地进行调试. 如何重用天文函数中可用的TraceWriter对象来记录外部引用库中的信息?我尝试使用构造函数传递对象并引用TraceWriter类(web.http.tracing).我没有运气,因为班级似乎不一样.解决方法 短版
使用 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,并且惊讶于还有一个处理方法.微软真的可以通过创建一个标准的方法,或至少给我们一个很好的干净的界面,错误,警告,信息,详细等.

我将创建自己的界面,并包装我的应用程序记录器和Azure,以便我可以注入我所需要的,而不会在我的代码中进一步引起头痛.这也将为未来突破性变化所造成的潜在痛苦提供一些保护.

无论如何,我离题,然后我跟踪Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter到Azure Functions / Webjobs scripting GitHub repo,然后到Nuget包.我已经测试了这一点,将Azure功能记录器传递到外部程序集并继续登录到Azure功能环境.

这是一个例子:

using Microsoft.Azure.WebJobs.Host;public static voID TryLog(TraceWriter azureFunctionsLogger){    azureFunctionsLogger.Info("************** IT WORKED **************");}

我喜欢Azure功能的潜力,但它仍然有点不成熟,过于复杂.

我希望这有帮助.

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

它写入Azure功能记录器或标准的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# – Azure函数在外部库中使用TraceWriter进行日志记录全部内容,希望文章能够帮你解决c# – Azure函数在外部库中使用TraceWriter进行日志记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1237581.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存