如何通过VS断点操作在外部程序集中访问自定义记录器 [英] How to access a custom logger, from a VS breakpoint action, in an external assembly

查看:58
本文介绍了如何通过VS断点操作在外部程序集中访问自定义记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio具有一个非常酷的功能,它允许您在遇到断点时将消息记录到输出窗口中。更酷的是,您可以通过用大括号括起来的实时值来引用它们。但是,等等:还有更多;您甚至可以调用具有任意签名的方法(传递参数并返回值)。返回的任何值都将写在输出窗口中。

Visual studio has a really cool feature in that allows you to log a message to the output window when a breakpoint is hit. Even more cool, you can reference live values by surrounding them with curly brackets. But wait: there's more; you can even call a method, with arbitrary signature (passing arguments and returning a value). Whatever value is returned is written in the output window. Awsome.

在VS 2013中,这是当命中功能

和在VS 2015(和2017 RC)中,它称为动作。

In VS 2013, this is the "When Hit" feature and in VS 2015 (and 2017 RC), it's called "Action".

我试图理解WPF源代码中发生了什么,因此我使用 Action设置断点以输出跟踪信息。但是,由于WPF不会限制我感兴趣的值的类型(它们以 object 的形式传递并根据上下文推断出的类型),因此这些值更常见而不是将其记录为错误,因为断点操作不了解此上下文,因此不了解预期的类型。

I am trying to understand what's going on in the WPF source code, so I set breakpoints with an "Action" to output trace info. But, because WPF is not constraining the types of the values I'm interested in (they are passed as object and the type inferred according to context), the values are more often than not logged as an error because the breakpoint Action is not aware of this context and therefore not aware of the type to expect.

我要解决的计划是从断点Action调用自定义记录器,并传递足以使记录器能够解析值类型的参数。

自定义记录器是MainWindow局部类的成员。

My plan to deal with this is to call a custom logger from the breakpoint Action and pass arguments sufficient for the logger can resolve the type of the value.
The custom logger is a member of my MainWindow partial class.

我已验证通过在我的MainWindow子类中设置断点操作来实现此目的,它引用了logger成员并传递了参数没有问题。这对于我的应用程序中的断点很好,但是我想在其他程序集中的断点上使用它。

I verified that this works by setting a breakpoint action in my MainWindow partial class, it references the logger member and passes arguments no problem. This is fine for breakpoints within my app but I want to use it on breakpoints in other assemblies.

如上所述,当断点在我自己的程序集中时,此方法工作正常,但对于第三方程序集中设置的断点(如wpf源代码)失败。显然,需要对对象进行限定,以便调试器可以解析它。

As stated above, this works fine when the breakpoint is in my own assembly but fails for breakpoints set in third party assemblies (like the wpf source code). Obviously the object needs to be qualified so that the debugger can resolve it.

我如何限定它,如何引用窗口实例以及要查找的成员?

我想我可以使它成为静态单例,但我可能需要多个记录器。

How do I qualify it, how do I reference the instance of the window and therefore the member I'm looking for?
I guess I could make it a static singleton but I might need more than one logger.

推荐答案

我发现可以引用我的记录器。例如,如果我的主窗口局部类看起来像这样……

I figured out that I can reference my logger. For example, if my main window partial class looks like this...

namespace EventSetterNull_SO_41604891_2670182
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

...然后我可以像这样引用它。 ..

...then I can reference it like this...

((EventSetterNull_SO_41604891_2670182.MainWindow)System.Windows.Application.Current.MainWindow)

所以,我的断点就像...

So, my breakpoint is like...

然后,我可以在例如System.Windows.Markup.WpfXamlLoader.TransformNodes中设置断点,将xamlReader传递给我的记录器,然后注销解析器从中生成的节点结构

Then, I can set breakpoints in, for example, System.Windows.Markup.WpfXamlLoader.TransformNodes, pass the xamlReader to my logger, and log out the node structure that the parser generates from the baml.

<Window x:Class="EventSetterNull_SO_41604891_2670182.BuildInXaml"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EventSetterNull_SO_41604891_2670182"
        mc:Ignorable="d"
        Title="BuildInXaml" Height="350" Width="525">
    <Window.Resources>
        <SetterBaseCollection x:Key="ButtonStyleSetters">
            <Setter Property="FrameworkElement.Height" Value="30"></Setter>
        </SetterBaseCollection>
    </Window.Resources>

    <Button Name="Button1"
            local:Behaviours.StyleSetters="{StaticResource ButtonStyleSetters}" />



输出窗口



Output window

"Line:    6    NamespaceDeclaration    http://schemas.microsoft.com/winfx/2006/xaml/presentation"
"Line:    6    NamespaceDeclaration    http://schemas.microsoft.com/winfx/2006/xaml"
"Line:    6    NamespaceDeclaration    http://schemas.microsoft.com/expression/blend/2008"
"Line:    6    NamespaceDeclaration    http://schemas.openxmlformats.org/markup-compatibility/2006"
"Line:    6    NamespaceDeclaration    clr-namespace:EventSetterNull_SO_41604891_2670182;assembly=EventSetterNull-SO-41604891-2670182"
"Line:    6    StartObject    BuildInXaml" 
"Line:    6        StartMember    Title"
"Line:    6            Value    BuildInXaml"
"Line:    6        EndMember    "
"Line:    8        StartMember    Height"
"Line:    8            Value    System.Windows.Baml2006.TypeConverterMarkupExtension"
"Line:    8        EndMember    "
"Line:    8        StartMember    Width"
"Line:    8            Value    System.Windows.Baml2006.TypeConverterMarkupExtension"
"Line:    8        EndMember    "
"Line:    8        StartMember    Resources"
"Line:    9            GetObject    System.Windows.Baml2006.Baml2006SchemaContext"
"Line:    9                StartMember    DeferrableContent"
"Line:    9                    Value    System.IO.MemoryStream"
"Line:    9                EndMember    "
"Line:    9            EndObject    "
"Line:   15        EndMember    "
"Line:   17        StartMember    Content"
"Line:   17            StartObject    Button" 
"Line:   17                StartMember    ConnectionId"
"Line:   17                    Value    1"
"Line:   17                EndMember    "
"Line:    0                StartMember    Name"
"Line:    0                    Value    Button1"
"Line:    0                EndMember    "
"Line:   17                StartMember    StyleSetters"
"Line:   17                    Value    System.Windows.StaticResourceExtension"
"Line:   17                EndMember    "
"Line:   10                StartObject    SetterBaseCollection" 
"Line:   10                    StartMember    _Items"
"Line:   11                        StartObject    Setter" 
"Line:   11                            StartMember    Property"
"Line:   11                                Value    Height"
"Line:   11                            EndMember    "
"Line:   11                            StartMember    Value"
"Line:   11                                Value    30"
"Line:   11                            EndMember    "
"Line:   11                        EndObject    "
"Line:   11                    EndMember    "
"Line:   11                EndObject    "
"Line:   18            EndObject    "
"Line:   18        EndMember    "
"Line:   18    EndObject    "

所以,我可以确切地看到wpf如何看到xaml,如果拧紧了它就很方便...

So, I can see exactly how wpf sees the xaml, which is handy if it screws up...

<Window x:Class="EventSetterNull_SO_41604891_2670182.BuildInXaml"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EventSetterNull_SO_41604891_2670182"
        mc:Ignorable="d"
        Title="BuildInXaml" Height="350" Width="525">
    <Window.Resources>
        <SetterBaseCollection x:Key="ButtonStyleSetters">
            <Setter Property="FrameworkElement.Height" Value="30"></Setter>
            <EventSetter Event="ButtonBase.Click"  Handler="StyleClick"
                          HandledEventsToo="False" />
        </SetterBaseCollection>
    </Window.Resources>

    <Button Name="Button1"
            local:Behaviours.StyleSetters="{StaticResource ButtonStyleSetters}" />



输出窗口



Output window

"Line:    6    NamespaceDeclaration    http://schemas.microsoft.com/winfx/2006/xaml/presentation"
"Line:    6    NamespaceDeclaration    http://schemas.microsoft.com/winfx/2006/xaml"
"Line:    6    NamespaceDeclaration    http://schemas.microsoft.com/expression/blend/2008"
"Line:    6    NamespaceDeclaration    http://schemas.openxmlformats.org/markup-compatibility/2006"
"Line:    6    NamespaceDeclaration    clr-namespace:EventSetterNull_SO_41604891_2670182;assembly=EventSetterNull-SO-41604891-2670182"
"Line:    6    StartObject    BuildInXaml" 
"Line:    6        StartMember    Title"
"Line:    6            Value    BuildInXaml"
"Line:    6        EndMember    "
"Line:    8        StartMember    Height"
"Line:    8            Value    System.Windows.Baml2006.TypeConverterMarkupExtension"
"Line:    8        EndMember    "
"Line:    8        StartMember    Width"
"Line:    8            Value    System.Windows.Baml2006.TypeConverterMarkupExtension"
"Line:    8        EndMember    "
"Line:    8        StartMember    Resources"
"Line:    9            GetObject    System.Windows.Baml2006.Baml2006SchemaContext"
"Line:    9                StartMember    DeferrableContent"
"Line:    9                    Value    System.IO.MemoryStream"
"Line:    9                EndMember    "
"Line:    9            EndObject    "
"Line:   15        EndMember    "
"Line:   17        StartMember    Content"
"Line:   17            StartObject    Button" 
"Line:   17                StartMember    ConnectionId"
"Line:   17                    Value    1"
"Line:   17                EndMember    "
"Line:    0                StartMember    Name"
"Line:    0                    Value    Button1"
"Line:    0                EndMember    "
"Line:   17                StartMember    StyleSetters"
"Line:   17                    Value    System.Windows.StaticResourceExtension"
"Line:   17                EndMember    "
"Line:   10                StartObject    SetterBaseCollection" 
"Line:   10                    StartMember    _Items"
"Line:   11                        StartObject    Setter" 
"Line:   11                            StartMember    Property"
"Line:   11                                Value    Height"
"Line:   11                            EndMember    "
"Line:   11                            StartMember    Value"
"Line:   11                                Value    30"
"Line:   11                            EndMember    "
"Line:   11                        EndObject    "
"Line:   12                        StartObject    EventSetter" 
"Line:   12                            StartMember    Event"
"Line:   12                                Value    System.Windows.Baml2006.TypeConverterMarkupExtension"
"Line:   12                            EndMember    "
"Line:   12                            StartMember    Event"
Exception thrown: 'System.Xaml.XamlDuplicateMemberException' in System.Xaml.dll

如您所见,wpf感到困惑,并在EventSetter对象中找到第二个Event成员,但不存在,并抛出错误。



这是wpf中的错误。

As you can see, wpf is getting confused and finding a second Event member in the EventSetter object, that's not there and throwing an error.

This is a bug in wpf.

这篇关于如何通过VS断点操作在外部程序集中访问自定义记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆