How do you use a .Net TextWriter with Azure WebJobs


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


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);

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



By : justsayno

This video can help you solving your question :)
By: admin