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?
I used a technique similar to the one described in the first link you provided. It can be resumed by the following steps:
TraceListener
that throws instead of loggingPresentationTraceSources.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.
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.