Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:51:05+00:00 2026-05-25T11:51:05+00:00

I have a number of classes that call say Trace.WriteLine(LogSomethingClassSpecific), in various methods all

  • 0

I have a number of classes that call say Trace.WriteLine(“LogSomethingClassSpecific”), in various methods all over the place. Now I would like some of those classes to make all their Trace calls like this: Trace.WriteLine(“LogSomethingClassSpecific”, “CategoryA”)

But! I would like to achieve this externally from a single categorizer class; without having to find and modify every Trace call in each of the specific classes.

Sample Architecture

  • Class1,Class2,Class3 which may or may not use Trace calls

  • Separate ControllerClass that makes all the existing Trace calls in say Class2 and Class3 be called with the added argument “CategoryA”. While retaining the flexibility to easily change the target classes.

How could that be achieved?

Might it be possible using some Attribute combined with an Aspect Oriented Programming (AOP) library?
e.g.
the controlling class could specify which classes to target:

[assembly: Categorizer("CategoryA", AttributeTargetTypes = "Namespace.ClassB")]

And then intercept all Trace.WriteLine calls with PostSharp

But I dont know of a way to extract the call context to determine whether the calling class has been marked to include “CategoryA”?

Or are there alternative ways to achieve this aim?

Thankful for any thoughts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:51:05+00:00Added an answer on May 25, 2026 at 11:51 am

    There are a few ways to go about this, but i’m assuming you’re calling Trace in places like if/else constructs so here is how you would go about doing what you’re asking for (at least, what I think you’re asking for)

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using PostSharp.Aspects;
        using System.Diagnostics;
    
        [assembly: ConsoleApplication2.TraceIntercept(AttributeTargetAssemblies = "System", AttributeTargetTypes = "System.Diagnostics.Trace")]
    
        namespace ConsoleApplication2
        {
            class Program
            {
                static void Main(string[] args)
                {
                    ExampleA ex = new ExampleA();
                    ex.Method1();
    
                    Console.ReadKey();
                }
    
            }
    
            public class ExampleA
            {
                public void Method1()
                {
                    Trace.Write("Test");
                }
    
            }
    
    [Serializable]
        [TraceIntercept(AttributeExclude = true)]
        public class TraceIntercept : MethodInterceptionAspect
        {
            private bool addArgument;
            private string typeName = string.Empty;
    
            public override void OnInvoke(MethodInterceptionArgs args)
            {
                CheckInvocationPoint();
    
                if (addArgument)
                {
                    //Do work. Change arguments, etc.
                }
    
                args.Proceed(); // Proceed with Trace
            }
    
            private void CheckInvocationPoint()
            {
                if (string.IsNullOrEmpty(this.typeName))
                {
                    StackTrace s = new StackTrace();
                    StackFrame f = s.GetFrame(2);
                    string className = f.GetMethod().DeclaringType.Name;
    
                    if (classsName.Equals("ExampleA"))
                    {
                        addArgument = true;
                    }
                }
            }
        }
    }
    

    This will intercept calls to the System.Diagnostics.Trace methods and will instead invoke the TraceIntercept.OnInvoke method where you can manipulate the Trace invocation. The way this works is PostSharp will simply replace calls to Trace.Write with calls to the aspect.

    Edit: As far as I know, there isn’t a way to acquire the actual invocation point of the target method. This means that you have to do some reflection at runtime. I’ve updated the code to use the stack trace. You only have to do this once (per type) since the aspect lifetime is going to be per type not per instance so you only take a hit once. It’s not how I would do it, but I assume this will be for debugging purposes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have a number of data classes representing various entities. Which is better: writing
I have a native/unmanaged C++ library with a number of classes that I would
I have a number of classes that are decorated with DebuggerDisplayAttribute. I want to
I have a project with a number of different classes querying and modifying data
In Ruby I have often written a number of small classes, and had a
I'm working with a number of 'helper' classes, which affectively have a bunch of
Does Information Hiding mean I should minimize the number of properties my classes have?
I have a manager class with a number of sub-classes. I find one particular
I have two classes. Looking to be able to grab a variable that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.