PandaWood/ExceptionReporter.NET

Screenshot not taken in silent mode

PoLaKoSz opened this issue · 6 comments

Hi!

Thank You @PandaWood for this awesome library! Today I had the chance to try it out and I found a little bug:

When I try to send silently a crash report invoking the following

/// <summary>
/// Send the report without showing a dialog (silent send)
/// <see cref="ReportSendMethod"/>must be set to SMTP or WebService, else this is ignored (silently)
/// </summary>
/// <param name="exceptions">The exception/s to include in the report</param>
public void Send(params Exception[] exceptions)
{
_reportInfo.SetExceptions(exceptions);
var generator = new ExceptionReportGenerator(_reportInfo);
if (_reportInfo.SendMethod == ReportSendMethod.WebService)
{
generator.SendReportToWebService();
}
else if (_reportInfo.SendMethod == ReportSendMethod.SMTP ||
_reportInfo.MailMethod == ExceptionReportInfo.EmailMethod.SMTP) // backwards compatibility
{
generator.SendReportByEmail();
}
else if (_reportInfo.SendMethod == ReportSendMethod.SimpleMAPI ||
_reportInfo.MailMethod == ExceptionReportInfo.EmailMethod.SimpleMAPI) // backwards compatibility
{ // this option must be last for compatibility because osbsolete MailMethod.SimpleMAPI was previously 0/default
// can't do silently so do nothing
}
}

code the crash report not include the screenshot when I set the ExceptionReportingInfo.TakeScreenshot
to true.

Solutions

It can be fixed easily calling the ScreenshotTaker.TakeScreenShot(); method when settings up the ExceptionReporter class

or

with this commit it can be fixed internally. If You like it I will happily make a PR 😄 (I had to make the ExceptionReportInfo to public to access inside the UnitTest).

Code to reproduce the bug

using ExceptionReporting;
using System;
using System.Threading;

namespace ConsoleApp
{
    public class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                RunApp();
            }
            catch (Exception ex)
            {
                var exceptionReporter = new ExceptionReporter();
                ConfigureSmtpEmail(exceptionReporter.Config);
                
                exceptionReporter.Config.TakeScreenshot = true;
                
                // this is the quick fix
                //exceptionReporter.Config.ScreenshotImage = ScreenshotTaker.TakeScreenShot();

                exceptionReporter.Send(ex);
            }

            Thread.Sleep(10 * 1000);
        }

        private static void RunApp()
        {
            InitializeViewModels();
        }

        private static void InitializeViewModels()
        {
            ApplicationUpdater();
        }

        private static void ApplicationUpdater()
        {
            throw new Exception("Can not get the latest version from GitHub.com",
                new Exception("Encoding failed"));
        }

        private static void ConfigureSmtpEmail(ExceptionReportInfo config)
        {
            config.SendMethod = ReportSendMethod.SMTP;

            config.SmtpServer         = "________";
            config.SmtpPort           = 587;
            config.SmtpUsername       = "________";
            config.SmtpPassword       = "________";
            config.SmtpFromAddress    = "________";
            config.EmailReportAddress = "________";
            config.SmtpUseSsl         = true;
        }
    }
}

Failing Attacher UnitTest

using ExceptionReporting.Core;
using ExceptionReporting.Mail;
using Moq;
using NUnit.Framework;

namespace ExceptionReporting.Tests
{
    [Test]
    public void Should_Make_Screenshot_In_Silent_Mode__Issue_26()
    {
        var attacher = new Attacher(new ExceptionReportInfo { TakeScreenshot = true });

        attacher.AttachFiles(_iattach.Object);

        Assert.IsTrue(attacher.Config.ScreenshotAvailable);
    }
  }
}

I would like to help You develop this library but with the indent_size = 2 config I can't really read and write the source code.
Is there a way to use my preferred indention and command Visual Studio to convert my 4 space intention to 2 (or whatever is in the .editorconfig file) when I close the file and when I reopen it it show me with 4 space intention again?

Sorry for my bad English grammar 😢

Thanks a lot @PoLaKoSz

You've provided everything needed!

I could copy your changes in but it would be good for you to get a credit for being a "Contributor" ;-)
So for that reason I'm very happy to accept a Pull Request.

Just be sure to squash your changes into a single commit on a branch (not master) before submitting.

<removed advice that turned out to be wrong on how to override .editoconfig>

Actually correction, to override .editorconfig toggle off the Follow project coding conventions option in Tools > Options > Text Editor
https://docs.microsoft.com/en-us/visualstudio/ide/media/coding_conventions_option.png

I don't know yet why, but when I uncheck the box, when I copy-paste code or just use the tab it still use 2 space not 4 (this is in my settings) - I need to delete .editorConfig every time after a commit. Can we remove the .editorConfig file?