soygul / NBug

Automated bug reporting library for .NET
http://soygul.com/nbug
MIT License
191 stars 78 forks source link

Reports not sent when configured from the code #52

Closed datalandsoftware closed 9 years ago

datalandsoftware commented 9 years ago

Hi people,

I'm trying to configure NBug through code, by using these lines:

#if DEBUG
    NBug.Settings.StoragePath = NBug.Enums.StoragePath.CurrentDirectory;
#else
    NBug.Settings.StoragePath = NBug.Enums.StoragePath.IsolatedStorage;
#endif
    NBug.Settings.UIProvider = NBug.Enums.UIProvider.Custom;
    NBug.Settings.CustomUIEvent += Settings_CustomUIEvent;
    NBug.Settings.AddDestinationFromConnectionString("Type=Redmine;ApiKey=...;ProjectId=...;Url=http://...;");
    NBug.Settings.ReleaseMode = true;

All works normally, except the report is never sent and zip file never deleted. If I use NBug.config file with exact the same destination string, all is good. Watch shows that Destination list looks good, other settings/defaults correspond to .config file, I even tried to ditch AddDestinationFromConnectionString and manually create an item), with or without ReleaseMode, always the same.

Am I missing something important here?

Thank you in advance!

soygul commented 9 years ago

Assuming that is a WPF app, you need these:

AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
Application.Current.DispatcherUnhandledException += NBug.Handler.DispatcherUnhandledException;

In any case, I strongly suggest that you use NBug.config file anyway. System culture etc. all mess up with config parser in the code but always works well with the external config files.

datalandsoftware commented 9 years ago

Soygul,

Thank you for your time, much appreciated. Unfortunately, the same thing. I see default error window and zip file is created, but it is never sent nor deleted after restart and wait.

What do you mean by "issue with the library"? Is there some kind of workaround?

Thanks!

soygul commented 9 years ago

Yep, reported here before: https://github.com/soygul/NBug/issues/35

soygul commented 9 years ago

One thing you can do to help is to grab the project source code, go to this line: https://github.com/soygul/NBug/blob/08d98274fea10595cb042acc3e54bb0bd6d7d58d/NBug/Settings.cs#L781

and change

"add", new XAttribute("name", "NBug.Properties.Settings.Connection" + number), new XAttribute("connectionString", Encrypt(content))));

into

"add", new XAttribute("name", "NBug.Properties.Settings.Destination" + number), new XAttribute("connectionString", Encrypt(content))));

and see if that solves the problem.

datalandsoftware commented 9 years ago

Sorry, no change. But isn't this part of code for generating config files, basically used by Configurator? My problem is not related.

Anyway, now that I'm encouraged to get into the source code, :) I'll try to understand what's going on and post here if I find something.

soygul commented 9 years ago

To be honest, I'm also taking guesses here:) I turned to OS X so don't have Visual Studio so can't debug the lib.. If you find anything let me know, I'll try and be the human compiler. On May 27, 2015 6:44 PM, "vradmilovic" notifications@github.com wrote:

Sorry, no change. But isn't this part of code for generating config files, basically used by Configurator? My problem is not related.

Anyway, now that I'm encouraged to get into the source code, :) I'll try to understand what's going on and post here if I find something.

— Reply to this email directly or view it on GitHub https://github.com/soygul/NBug/issues/52#issuecomment-105989188.

datalandsoftware commented 9 years ago

Sorry for the delay, I had some "offline" things to do in the meantime.

Anyway, the problem has several components combined:

  1. Most important one: Dispatcher class is created and executed before App gets to initialize Destinations...
  2. ...and before we set ReleaseMode = true, meaning that...
  3. ...RemoveThreadSleep is still true (startup delay skipped) and ...
  4. ...DispatcherIsAsynchronous is false (so even if there is a delay, it would block application preventing it to initialize properly anyway)

The problem doesn't happen when using .config file, because settings are loaded before dispatching.

Now, there are few ways to fix this, but probably not all are good and "nice", without breaking an intended functionality. I'm quite new with Visual Studio and C# and still figuring out all debugging bells and whistles, so I haven't found where initial dispatching is called from yet - maybe there's a simple fix?

soygul commented 9 years ago

Dispatcher is initialized here: https://github.com/soygul/NBug/blob/master/NBug/Handler.cs#L30

Which means the moment you attach any bug handlers the dispatcher starts sending queued bug reports. So attaching handlers after setting up NBug configuration might do the trick:

...
...
NBug.Settings.AddDestinationFromConnectionString("Type=Redmine;ApiKey=...;ProjectId=...;Url=http://...;");
NBug.Settings.ReleaseMode = true;

AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
Application.Current.DispatcherUnhandledException += NBug.Handler.DispatcherUnhandledException;
datalandsoftware commented 9 years ago

Yes, that did it, brilliant!

Soygul, thank you very much for your help, not to mention all previous great work you did to create NBug.

All the best!

soygul commented 9 years ago

Cool! Interesting that integrations tests for code based configuration never failed on my machine, maybe related to CPU speed... I'll update all the docs to have configuration before handlers.

Thanks for the effort!