如何在Azure WebJobs中使用.Net TextWriter

时间:2015-03-24 23:42:42

标签: c# azure azure-webjobs textwriter

我想做一些简单的调试,Azure WebJobs似乎不再支持Console.Writeline。

我知道使用TextWriter类就是答案,我只是将它注入到我的方法中。我不明白的是我怎么称呼这种方法。我无法使我的Main方法签名无效并将其注入其中。

我错过了什么?

public static void Main(TextWriter log)
{
    //This is is not valid
}

3 个答案:

答案 0 :(得分:4)

对于连续的webjob,你可以这样做:

import pygame, sys

def process1(bug):
    #PROCESSING

    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if keys[pygame.K_d]:
            bug.velx = 5
        if keys[pygame.K_a]:
            bug.velx = -5
        if keys[pygame.K_w]:
            bug.jumping = True
        #attacks
        if keys[pygame.K_1]:
            bug.velx = 50
        if keys[pygame.K_2]:
            bug.velx = -50
        else:
            bug.velx = 0

def process2(bug):
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if keys[pygame.K_d]:
            bug.velx = 5
        if keys[pygame.K_a]:
            bug.velx = -5
        if keys[pygame.K_w]:
            bug.jumping = True
        #attacks
        if keys[pygame.K_1]:
            bug.velx = 50
        if keys[pygame.K_2]:
            bug.velx = -50
        else:
            bug.velx = 0

答案 1 :(得分:3)

虽然上述方法是正确的,但您也可以创建自己的自定义TraceWriter实例,以拦截应用程序中的Trace调用。

TraceWriter类的示例:

using System.Diagnostics;
using Microsoft.Azure.WebJobs.Host;

namespace MiscOperations
{
    /// <summary>
    /// Custom <see cref="TraceWriter"/> demonstrating how JobHost logs/traces can
    /// be intercepted by user code.
    /// </summary>
    public class CustomTraceWriter : TraceWriter
    {
        public CustomTraceWriter(TraceLevel level)
            : base(level)
        {
        }

        public override void Trace(TraceEvent traceEvent)
        {
            // handle trace messages here
        }
    }
}

然后在JobHostConfuration设置中,在控制台应用程序的main方法中注册TraceWriter类的实例。即我在Program.cs。

JobHostConfiguration config = new JobHostConfiguration()
{
    NameResolver = new ConfigNameResolver(),
};

// Demonstrates how the console trace level can be customized
config.Tracing.ConsoleLevel = TraceLevel.Verbose;

// Demonstrates how a custom TraceWriter can be plugged into the
// host to capture all logging/traces.
config.Tracing.Tracers.Add(new CustomTraceWriter(TraceLevel.Info));

JobHost host = new JobHost(config);
host.RunAndBlock();

通过这种方式,您可以使用跟踪执行其他操作,例如写入外部服务或创建警报。

取自Azure SKD Samples Gitthub

CustomTraceWriter.cs

Program.cs

答案 2 :(得分:2)

我认为您需要向签名添加QueueTrigger属性,Azure会在事件发生时调用它。例如:

public static void ProcessQueueMessage([QueueTrigger("logqueue")] string logMessage, TextWriter logger)
{
    logger.WriteLine(logMessage);
}

请参阅此链接:https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/#trigger