.NET TraceListener is not working with Azure Functions
See original GitHub issueWe’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:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Turns out there is a very simple way to enable this: just add this line at the top of your function:
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.