c# – Azure函数在外部库中使用TraceWriter进行日志记录
如何重用天文函数中可用的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()); 其中给出以下内容:
我也期待着一个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(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |