tags:

views:

56

answers:

1

I have a requirement where I want to trace what file is being processed by .net runtime. I mean if it is processing usercontrol x.ascx then it should return the whole path of that, and if it is processing usercontrol y.ascx it should return that.

There are some candidates properties.

Request.AppRelativeCurrentExecutionFilePath

or

TemplateControl.AppRelativeVirtualPath. 

Can somebody help me with this, or is there any other property that can give me the path.

+1  A: 

Request.AppRelativeCurrentExecutionFilePath should do what you need. Is it producing unexpected results?

Update

You should be able to retrieve any UserControl(s) currently being executed by enumerating the Controls collection of the current page handler. Assuming an external context, here's an example that should work:

public static string[] GetCurrentUserControlPaths() {
    if(HttpContext.Current == null) return new string[0];
    if(!(HttpContext.Current.Handler is Page)) return new string[0];

    var page = (HttpContext.Current.Handler as Page);
    var paths = ControlAggregator(page, c => c is UserControl).Cast<UserControl>().Select(uc => uc.AppRelativeVirtualPath);

    if(page.Master != null) {
        paths.Concat(ControlAggregator(page.Master, c => c is UserControl).Cast<UserControl>().Select(uc => uc.AppRelativeVirtualPath));
    }

    return paths.ToArray();
}

public static Control[] ControlAggregator(this Control control, Func<Control, bool> selector) {
    var list = new List<Control>();

    if (selector(control)) {
        list.Add(control);
    }

    foreach(Control child in control.Controls) {
        list.AddRange(ControlAggregator(child, selector));
    }

    return list.ToArray();
}
Nathan Taylor
This giving me only .aspx name not the user control getting processed. I could do that by defining an extension method on user control and the page may be.
Mohit
@Mohit please see the update I posted.
Nathan Taylor
But, this would give me the list of user controls inside a page, it would not give me the user control being processed right now.
Mohit
Is this for some sort of remote monitoring or is it an internal function of the website? Is tracing perhaps what you're looking for instead?
Nathan Taylor
we want to do some sort of usability analysis and measure which navigation method , user is using the most. Like is he clicking on the top tabs more or the left Menu or bottom toolbar. Simplest thing is append a query string at the end of the link and then look at the IISLog. For appending the control name I thought of writing a static method that just takes a url brushes it and returns the url with the control name, as of now it is an extension method on user control.Since, I could get the name without passing the control instance.
Mohit
It may be more practical to simply place a Trace statement or write to a logger in a custom base class of an UserControl that spits outs the path when the control handles a load.
Nathan Taylor