Azure WebJobs SDK是否支持将TextWriter日志推送到App Insights?

时间:2016-12-15 00:00:27

标签: azure azure-webjobs azure-webjobssdk

使用Azure WebJobs SDK,向函数添加日志记录的过程相对简单:向触发的函数添加TextWriter参数,并写入它。而已。

然后,SDK将在WebJobs仪表板中将这些日志与其执行实例相关联并显示,这样可以为您的webjobs操作提供相对数据丰富且无摩擦的视图。

虽然此数据被复制到用户可访问的Azure存储Blob容器中,但是需要更多自定义代码来定期将这些日志推送到App Insights,这是不可取的。

寻找有关如何将通过注入的TextWriter推送的所有日志推送到AppInsights(或OMS,就此而言)的想法或解决方案,完成webjobs执行/触发器实例元数据,从而实现统一的消费体验各种日志分析。

基于在WebJobs SDK中跟踪的this Feature,我假设现在这是不可能的?很久以前我试着注入我自己的TextWriter实例,但我不得不分叉WebJobs SDK并使用我的自定义程序集改变了很多架构。

3 个答案:

答案 0 :(得分:2)

您可以编写将日志发送到AppInsights的自定义<html id='html'> <head> <title>Maze server</title> <center> <hr><br> <h1>Welcome to my test server!</h1> <br><hr> <p>This is some text to test</p> </center> <script type='text/JavaScript'> document.getElementById('html').style.hide = "true"; document.onload = function(){ document.getElementById('html').style.hide = "false"; } var clicks = 0; function clickButton(){ clicks++; document.getElementById('clicks').innerHTML = clicks; } </script> </head> <script src="/socket.io/socket.io.js"></script> <script> var socket = io(); </script> <body> <center> <button onClick='clickButton();'>Im a button</button> <p>You've clicked me <span id='clicks'>0</span> times. Ouch!</p> <br><hr> <p>Test canvas:</p> <canvas id='canvas' width='400' height='200'></canvas> <script src='pong.js' type='text/JavaScript'></script> <br> <br> <button onClick='bounce();'>Bounce!</button> <br> <button onClick='speed("x");'>Speed up X</button> <button onClick='speed("y");'>Speed up Y</button> <br> <button onClick='speed("x"); speed("y");'>Speed up both</button> <br> <button onClick='resetXY();'>Reset</button> <br><hr> </center> <p>"Copyrite" <strong><blink type='EasterEgg' mazeiness='true'>MazeOfEncryption</blink></strong> 2048. Because why the heck not.</p> </body> </html>

TraceWriter

在这个例子中,我注入了using System.Collections.Generic; using System.Diagnostics; using Microsoft.ApplicationInsights; using Microsoft.Azure.WebJobs.Host; public class AppInsightsTraceWriter : TraceWriter { private readonly TelemetryClient _telemetryClient; public AppInsightsTraceWriter(TraceLevel level, TelemetryClient telemetryClient) : base(level) { _telemetryClient = telemetryClient; } public override void Trace(TraceEvent traceEvent) { var eventTelemetry = new EventTelemetry() {Name = "WebjobTraceEvent"}; eventTelemetry.Properties.Add(traceEvent.Level.ToString(), traceEvent.ToString()); _telemetryClient.TrackEvent(eventTelemetry); } } 类,因为你的应用程序中只应该有一个TelemetryClient类的实例。

现在您只需配置TelemetryClient即可使用自定义编写器:

Jobhost

所以如果你有这样的功能:

// Initialize the webjob configuration.
var config = new JobHostConfiguration();

// Only one instance of the telemetry client is needed
var telemetryClient = new TelemetryClient() {InstrumentationKey = "MyInstrumentationKey"};

// Add the app insights tracer for webjob logs/traces.
config.Tracing.Tracers.Add(new AppInsightsTraceWriter(TraceLevel.Info, telemetryClient));

// Detect when the webjob shut down
var cancellationToken = new WebJobsShutdownWatcher().Token;
cancellationToken.Register(() =>
{
    // Before shut down, flush the app insights client.
    telemetryClient.Flush();
});

new JobHost(config).RunAndBlock();

每次使用log.WriteLine时,都会向App Insights发送一个事件。

注意:如果此示例还将来自JobHost的日志发送到AppInsights。

答案 1 :(得分:0)

我将分享在Azure Web Job中使用Application Insights的详细步骤,请参阅它。

  1. 在Azure门户中创建新的Azure应用洞察
  2. 在Visual Studio中创建Azure Web作业项目并进行安装 Microsoft.ApplicationInsights
  3. 设置检测密钥并发送 遥测

    public static void ProcessQueueMessage([QueueTrigger("queuename")] string message, TextWriter log)
    {
    
        TelemetryClient tc = new TelemetryClient();
    
        tc.InstrumentationKey = "key copied from Azure portal";
        tc.TrackTrace(message);
    
        tc.Flush();
    
        //log.WriteLine(message);
    }
    
  4. 本文档解释了how to monitor usage and performance in Windows Desktop apps,您可以参考它来了解如何在非Web应用程序中使用Azure Application Insights。此外,ApplicationInsights.Helpers.WebJobs也可能有所帮助。

答案 2 :(得分:0)

这太老了(不确定为什么这么久以后我决定把它放在边栏里),但是对于其他偶然发现的人,现在推荐使用应用洞察来监视webjob的执行情况。

在此处查看文档,该文档逐步介绍了将应用程序见解与网络职位相关联的过程。

此链接将引导您配置新的webjobs项目的日志记录部分。检查前面的部分,以确保您具有所有先决条件。 https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started#add-application-insights-logging

static async Task Main()
{
    var builder = new HostBuilder();
    builder.UseEnvironment(EnvironmentName.Development);
    builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddAzureStorage();
            });
    builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();

                // If the key exists in settings, use it to enable Application Insights.
                string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                if (!string.IsNullOrEmpty(instrumentationKey))
                {
                    b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
                }
            });
    var host = builder.Build();
    using (host)
    {
        await host.RunAsync();
    }
}