Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在外部库中使用TraceWriter记录Azure函数_C#_Logging_Azure Webjobs_Azure Functions - Fatal编程技术网

C# 在外部库中使用TraceWriter记录Azure函数

C# 在外部库中使用TraceWriter记录Azure函数,c#,logging,azure-webjobs,azure-functions,C#,Logging,Azure Webjobs,Azure Functions,如何重用Azure函数中可用的TraceWriter对象来记录外部引用库中的信息?我尝试使用构造函数传入对象并引用TraceWriter类(web.http.tracing)。我运气不好,因为课程看起来不一样 短版 使用中提供的Microsoft.Azure.WebJobs.Host.TraceWriter 或者,将函数构建为Web项目,您可以在本地进行调试 长版本 这里的问题是您使用了错误的TraceWriter 我在Azure函数中使用Azure函数记录器来输出记录器的类型 log.Info

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

短版 使用中提供的Microsoft.Azure.WebJobs.Host.TraceWriter

或者,将函数构建为Web项目,您可以在本地进行调试

长版本

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

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

log.Info(log.GetType().ToString());
其中给出了以下结论:

Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter

我也期待一个Web/Http TraceWriter,并惊讶地发现还有另一个实现需要处理。微软真的可以创建一个标准方法,或者至少为我们提供一个关于错误、警告、信息、详细信息等的干净界面。也许是.Net标准的东西。。。求你了

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

无论如何,我离题了,然后我跟踪了
Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter
,一直跟踪到,然后跟踪到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包

名称空间日志测试控制台
{
使用制度;
/// 
///通用日志接口的可移植性
/// 
公共接口ILogger
{
无效错误(字符串消息);
无效信息(字符串消息);
无效警告(字符串消息);
}
/// 
///Azure函数记录器
/// 
公共类AzureFunctionLogger:ILogger
{
私有静态Microsoft.Azure.WebJobs.Host.TraceWriter\u记录器;
public AzureFunctionLogger(Microsoft.Azure.WebJobs.Host.TraceWriter logger)
{
_记录器=记录器;
}
公共无效错误(字符串消息)
{
_记录器。错误(消息);
}
公共无效信息(字符串消息)
{
_logger.Info(消息);
}
公共无效警告(字符串消息)
{
_记录器。警告(消息);
}
}
/// 
///Windows跟踪记录器
/// 
公共类跟踪记录器:ILogger
{
公共跟踪记录器()
{
添加(新的System.Diagnostics.TextWriterTraceListener(Console.Out));
}
公共无效错误(字符串消息)
{
系统.诊断.跟踪.跟踪错误(消息);
}
公共无效信息(字符串消息)
{
系统.诊断.跟踪.跟踪信息(消息);
}
公共无效警告(字符串消息)
{
系统。诊断。跟踪。跟踪(消息);
}
公共无效警告(字符串格式,参数对象[]args)
{
系统.诊断.跟踪.跟踪(格式,args);
}
}
/// 
///您可以将其放在一个单独的项目中,并共享ILogger界面。
///从Azure函数或标准windows跟踪记录器传入相关记录器。
/// 
公开课
{
公共DoStuff(ILogger记录器)
{
信息(“我们正在登录到您传入的记录器!”);
}
}
公共课程
{
/// 
///样本使用
/// 
静态void Main(字符串[]参数)
{
//var loggerEnvironment=“AzureFunctions”;
var loggerEnvironment=“ConsoleApp”;
ILogger logger=null;
if(loggerEnvironment==“AzureFunctions”)
{
Microsoft.Azure.WebJobs.Host.TraceWriter azureFunctionLogger=null;
记录器=新的AzureFunctionLogger(AzureFunctionLogger);
}
else if(loggerEnvironment==“ConsoleApp”)
{
记录器=新的TraceLogger();
}
var doStuff=新的doStuff(记录器);
Console.ReadKey();
}
}
}

作为更新,Azure函数现在支持使用
ILogger
而不是
TraceWriter
,因此您可以使用实现
ILogger
的任何日志框架


请参阅和随后的。

如果我的回答正确,则让ILogger使用Azure功能所需的版本将是Microsoft.Azure.WebJobs 2.1.0-beta1。然而,我似乎无法使用ILogger而不是TraceWriter来运行Azure函数

关于使用ILogger开发Azure函数的信息和文档也很少。有没有人有更多的信息或提示让这项工作

我的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}");
        }
    }
}
使用适用于VS2017的Azure函数工具调试Azure函数时,我遇到以下错误:

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 (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

谢谢,从文档/代码中很难找到这一点!TraceWriter实现的接口是什么?从这里开始,按照自己的方式操作-
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 (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).