11

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 also tried this: http://msdn.microsoft.com/en-us/library/system.diagnostics.presentationtracesources.aspx

The problem is that it only works while running in the VS IDE. When I publish the app the errors are not caugth.

Does anybody know how to get the WPF Bindind errors and save to a file, by the published app, programatically, during runtime ?

Community
  • 1
  • 1
Tony
  • 16,527
  • 15
  • 80
  • 134
  • 1
    Why would you want such a thing? I mean catching binding errors is a good thing to do when developing but once your application is deployed ... are you affraid your clients will do reverse engineering on your app and implant binding errors? – Omri Btian Oct 09 '13 at 14:36
  • 1
    I agree that no errors should exist on published application, I know, but the application is not small, and I don´t know where the error is, so I think that some error catching on the published app to file would help. – Tony Oct 09 '13 at 14:58
  • I agree with the other comments. It is usually the other way around, things work great in a development environment and then fail deployed for some reason. This is a nice feature IMHO. – Rob Goodwin Oct 09 '13 at 14:59
  • @Tony Don't get me wrong. Error handling and client-side logging are super important to have on published applications in order to detect run-time errors, but BINDING errors does not appear out of the blue. either you data binding are set correctly, or they don't. – Omri Btian Oct 09 '13 at 15:40
  • 2
    I agree. The problem is that here the binding shows no problem, so I suspected that some runtime race condition could be causing some invalid Binding. Is is not a stable error. It is somewhat random. This is why I try to save those binding errors. – Tony Oct 09 '13 at 15:42
  • Have a look here: [How to detect broken WPF Data binding?](http://stackoverflow.com/questions/337023/how-to-detect-broken-wpf-data-binding) Similar thread to the one you linked, but with different solutions – Will Faithfull Oct 14 '13 at 17:48
  • The problem here seemed to be a Network Hub was turned into the wall with no protection against energy surges. Then there was a database connection error, then the error propagated to the property that was binded on WPF. – Tony Oct 24 '13 at 01:00

2 Answers2

8

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

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
  • And I've just noticed that Benoit has put this in a NuGet package, thanks Benoit... https://www.nuget.org/packages/WpfBindingErrors/ – Richardissimo Nov 11 '20 at 14:22
2

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.

Adassko
  • 5,201
  • 20
  • 37