Turn WPF Binding error into runtime exception. Not working on published Released app

前端 未结 2 2380
南笙
南笙 2021-02-20 18:25

I would like to log the DataBinding errors to a file. I Used the solution presented in this accepted anwser:

How can I turn binding errors into runtime exceptions?

相关标签:
2条回答
  • 2021-02-20 18:46

    I used a technique similar to the one described in the first link you provided. It can be resumed by the following steps:

    1. Derive a TraceListener that throws instead of logging
    2. Add your listener to PresentationTraceSources.DataBindingSource

    I tested with .NET Framework 4.0, 4.5 and 4.5.1; it works on both Debug and Release configurations.

    Please check the complete solution I pushed on GitHub, it includes a demo application and a unit test project.

    Exception in Visual Studio

    0 讨论(0)
  • 2021-02-20 18:52

    I'm little late to the party but I had same issue recently and dig a little into .NET sources.

    So the issue is that tracing is enabled only when one of the following conditions is met

    AvTrace.cs:

    private static bool ShouldCreateTraceSources()
    {
        return AvTrace.IsWpfTracingEnabledInRegistry() || AvTrace.IsDebuggerAttached() || AvTrace._hasBeenRefreshed;
    }
    

    So binding errors will be reported only if:

    • WPF tracing is enabled in registry (HKCU\Software\Microsoft\Tracing\WPF\ManagedTracing)

    • Debugger is attached (it doesn't matter if the application was compiled in Debug or Release mode)

    • Tracing sources has been refreshed

    The last one is tricky - tracing sources are refreshed when you manually update tracing sources using:

    PresentationTraceSources.DataBindingSource
    

    That's why it works in the solution provided by Benoit Blanchon

    but it will not work when you define your tracing sources in app.config file directly. If you want to create tracing sources, you need to manually call:

    PresentationTraceSources.Refresh();
    

    this will re-read app.config, but also will call internal AvTrace.OnRefresh() that will change the _hasBeenRefreshed flag and enable tracing.

    0 讨论(0)
提交回复
热议问题