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

前端 未结 2 2482
南笙
南笙 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: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.

提交回复
热议问题