How to add activity source to tracer provider dynamically for a single service?

Ravi Patel 0 Reputation points
2023-02-03T11:25:14.44+00:00

In the blog here, https://learn.microsoft.com/en-us/dotnet/core/diagnostics/distributed-tracing-instrumentation-walkthroughs , Its mentioned in the best practices for ActivitySource to consider creating a new one for each library rather than using a single source for whole assembly. If we follow this then how do we add these sources while configuring the trace provider. It is not guaranteed that we will have a list of all the source names at the time of configuration. Is there a way of adding these sources at runtime?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,406 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,201 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,284 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rena Ni - MSFT 2,061 Reputation points
    2023-02-06T06:07:09.6966667+00:00

    Hi @Ravi Patel,

    After testing the code in the blog, the source name seems depend on the namespace of your invoked method. So for dynamically get the source name, you can use the code below:

    Type myType = typeof(Program);   //The method exists in Program class, get the class type
    var source = myType.Namespace;
    

    If there has any problem, please let me know freely.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Rena

    0 comments No comments