4

I want to do some simple debugging, and Console.Writeline no longer seems to be supported by Azure WebJobs.

I know using a TextWriter class is the answer, and I just inject it into my method. What I don't understand is how I call that method. I cannot invalidate my Main method signature and inject it there.

What am I missing please?

public static void Main(TextWriter log)
{
    //This is is not valid
}
Russ Taylor
  • 360
  • 3
  • 10
  • you can debug via visual studio http://blogs.msdn.com/b/webdev/archive/2014/11/12/new-developer-and-debugging-features-for-azure-webjobs-in-visual-studio.aspx – Aravind Mar 25 '15 at 06:45
  • I use Console.Writeline and it works fine.Obviously after some time the output window on SCM site will be full and you'll not see more lines. But for small and simple "debug" it's fine. IMHO – Fabrizio Accatino Mar 25 '15 at 08:14
  • Console.Writeline does work fine, thank you. I thought that each time I published it would re-start my Web Job. It wasn't, and so the changes I were making (using Console.Writeline) we not being run. I'd still be interest if anyone can tell me how this TextWriter can be used. – Russ Taylor Mar 26 '15 at 12:52

3 Answers3

5

For continous webjob, you can do it like that :

class Program
{
    private static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Program).GetMethod("Start"));
        host.RunAndBlock();
    }

    [NoAutomaticTrigger]
    public static void Start(TextWriter textWriter)
    {

    }
}
Thomas
  • 24,234
  • 6
  • 81
  • 125
4

While the above are correct you can also create your own custom TraceWriter instance that intercepts Trace calls in your application.

Example of a TraceWriter class:

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
        }
    }
}

Then in your JobHostConfuration setup you register your instance of the TraceWriter class in the main method of the console application. I.e in 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();

This way you can do other things with the trace such as write to an external service or create alerts.

Taken from the Azure SKD Samples Gitthub

CustomTraceWriter.cs

Program.cs

sethreidnz
  • 665
  • 6
  • 19
2

I believe you need to add a QueueTrigger attribute to the signature and Azure will call it when an event occurs. For instance:

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

See this link: https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/#trigger

marckassay
  • 1,350
  • 1
  • 11
  • 19