question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET TraceListener is not working with Azure Functions

See original GitHub issue

We’re trying write a TraceListener within Azure Function that can intercept the traces and send to somewhere else, ElasticSearch for example. However, the listener below can’t intercept any traces.

using System;
using System.Diagnostics;
using System.Diagnostics.Tracing;

public static void Run(string input, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    var listener = new MyListener(log);

    Trace.TraceError("This is a message from trace . . . Error Level.");
    Trace.WriteLine("This is a message from trace . . . The write line method.");
}

public class MyListener : TraceListener
{
    private TraceWriter _log;

    public MyListener(TraceWriter log)
    {
        Trace.Listeners.Add(this);
        this._log = log;
        _log.Info("MyListener is initialized");
    }

    public override void Write(string message)
    {
        _log.Info("MyListener message received: " + message);
    }

    public override void WriteLine(string message)
    {
        _log.Info("MyListener message received: " + message);
    }
}

Expected behavior

“MyListener” can intercept traces and print the message in the log-streaming service

Actual behavior

Nothing shows up

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
davidebbocommented, Feb 21, 2017

Turns out there is a very simple way to enable this: just add this line at the top of your function:

#define TRACE
0reactions
davidebbocommented, Feb 22, 2017

I think there are maybe different issues here. The one tracked here is what OP @yantang-msft raised, where he was registering his own trace listener and it wasn’t getting called. I tested that exact scenario with the #define TRACE and it worked for me. Can you try that?

If it’s a different scenario that’s not working, let’s use a separate issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot see Trace logs when loglevel set to Trace in azure ...
The default log level in azure functions is Information so a trace won't pass through.
Read more >
Tip #1142: Tracing in Azure Functions MkII - CRM Tip of the Day
NET traces in Azure Functions is easy – just create your own TraceListener. ... Log writter for working with Azure Functions.
Read more >
Trace the flow in Cloud Services (classic) Application with ...
Add tracing messages to an Azure application to help debugging, measuring performance, monitoring, traffic analysis, and more.
Read more >
TraceListener Class (System.Diagnostics)
Provides the abstract base class for the listeners who monitor trace and debug output.
Read more >
Explore .NET trace logs in Application Insights
Select the Configure trace collection option. Note. No Application Insights menu or log collector option? Try Troubleshooting. Manual ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found