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

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

简短版本使用此nuget包中提供的Microsoft.Azure.WebJobs.Host.TraceWriter。

或者,将您的function构建为Web项目,您可以在本地进行调试。 你可以在这里找到一个样本 。

长版

你的问题是你使用错误的TraceWriter。

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

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

其中给出了以下内容:

Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter

我也期待着一个Web / Http TraceWriter,并且惊讶于还有另一个实现要处理。 微软可以真正做到创建一个标准的方法,或至少给我们一个漂亮的干净,错误,警告,信息,详细等界面。也许.Net标准…请。

我将创建自己的界面并包装我的应用程序记录器和Azure,以便我可以注入我需要的任何东西,而不会在我的代码中进一步引起麻烦。 这也将提供一些保护,免受未来重大变化带来的潜在痛苦。

无论如何,我离题了,然后我跟踪了Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter到Azure Functions / Webjobs脚本GitHub repo然后再到Nuget包。 我已对此进行了测试,并且可以将Azurefunction记录器传递到外部程序集并继续从那里登录到Azurefunction环境。

这是一个例子:

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

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

我希望这有帮助。

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

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

 namespace LoggingTestConsole { using System; ///  /// Generic logging interface for portability ///  public interface ILogger { void Error(string message); void Information(string message); void Warning(string message); } ///  /// Azure Functions logger ///  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); } } ///  /// Windows Trace logger ///  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); } } ///  /// 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. ///  public class DoStuff { public DoStuff(ILogger logger) { logger.Information("We are logging to logger you passed in!"); } } public class Program { ///  /// Sample usage ///  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(); } } } 

作为更新,Azure Functions现在支持使用ILogger而不是TraceWritter因此您可以使用任何实现ILogger日志记录框架。

请参阅GitHub问题和随后的Wiki文档 。

如果我是正确的,那么让ILogger使用Azure函数的必要版本将是Microsoft.Azure.WebJobs 2.1.0-beta1。 但是,我似乎无法使用ILogger而不是TraceWriter运行Azurefunction。

关于使用ILogger开发Azurefunction的信息和文档也非常少。 有没有人有更多的信息或技巧来使这个工作?

我的C#代码片段:

 using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.ServiceBus; using System; using Newtonsoft.Json; using Microsoft.Extensions.Logging; namespace Experimental.Functions { public static class ListenToEventFunction { [FunctionName("ListenToEventFunction")] public static void Run([EventHubTrigger("events", Connection = "EventHubConnectionString")]string myEventHubMessage, ILogger log) { log.LogInformation($"C# Event Hub trigger function processed a message: {myEventHubMessage}"); } } } 

使用Azurefunction工具VS2017调试我的Azurefunction时出现以下错误:

 A ScriptHost error has occurred Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.EnrichTelemetryLocation'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger. Make sure the parameter Type is supported by the binding. If you're using binding extensions (eg ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (eg config.UseServiceBus(), config.UseTimers(), etc.).