Skip to content

Elastic.Apm.Agent.AddFilter doesn't work in Global.asax's Application_Start() #1045

Description

@Pepo-BG

Describe the bug
In my MVC project, I wanted to add some global filters. To do that, I've added my code in the Global.asax Application_Start() method. Here is my code:

protected void Application_Start()
{
  //my other code
  Utils.ElasticUtilities.AddAPMGlobalFilter();
}
public class ElasticUtilities
    {
        public static void AddAPMGlobalFilter()
        {
            AgentDependencies.Logger = new ApmLoggerBridge();

            try
            {
                bool filterAdded = Elastic.Apm.Agent.AddFilter((Elastic.Apm.Api.ITransaction transaction) =>
                {
                    try
                    {
                        LogMessage("APM Transaction filter" + transaction.Id);
                        transaction.Labels.Add("UserID", MyCustomCode.UserID.ToString());
                        transaction.Labels.Add("EmailAddress", MyCustomCode.EmailAddress);
                    }
                    catch (Exception exc)
                    {
                        transaction.CaptureException(exc);
                        LogException(exc);
                    }
                    return transaction;
                });
                LogMessage("Filter added:" + filterAdded.ToString());
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
        }

        internal class ApmLoggerBridge : IApmLogger
        {
            public bool IsEnabled(LogLevel level)
            {
                return true;
            }

            public void Log<TState>(LogLevel level, TState state, Exception e, Func<TState, Exception, string> formatter)
            {
                string message = formatter(state, e);
                if (e == null)
                {
                    LogMessage(message);
                }
                else
                {
                    LogException(e);
                }
            }
        }

    }

But transactions aren't sent to the APM... The only exceptions that I received are:

Exception type:
System.UnauthorizedAccessException

Message:
Access to the registry key 'Global' is denied.

and

Source:
Microsoft.Diagnostics.Tracing.TraceEvent

Exception type:
System.UnauthorizedAccessException

Message:
Error Starting ETW:  Access Denied (Administrator rights required to start ETW)

The configuration is valid because the code above everything is logged to the APM correctly.
If I move my code from Application_Start() to Session_Start() it works, but I don't want to add a filter for each session.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions