Пример #1
0
        private void CreateReportZip(SerializableException serializableException, Report report)
        {
            // Test if it has NOT been more than x many days since entry assembly was last modified)
            if (Settings.StopReportingAfter < 0 ||
                File.GetLastWriteTime(Settings.EntryAssembly.Location).AddDays(Settings.StopReportingAfter).CompareTo(DateTime.Now) > 0)
            {
                // Test if there is already more than enough queued report files
                if (Settings.MaxQueuedReports < 0 || Storer.GetReportCount() < Settings.MaxQueuedReports)
                {
                    var reportFileName   = "Exception_" + DateTime.UtcNow.ToFileTime() + ".zip";
                    var minidumpFilePath = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Exception_MiniDump_" + DateTime.UtcNow.ToFileTime() + ".mdmp");

                    using (var storer = new Storer())
                        using (var zipStorer = ZipStorer.Create(storer.CreateReportFile(reportFileName), string.Empty))
                            using (var stream = new MemoryStream())
                            {
                                // Store the exception
                                var serializer = new XmlSerializer(typeof(SerializableException));
                                serializer.Serialize(stream, serializableException);
                                stream.Position = 0;
                                zipStorer.AddStream(ZipStorer.Compression.Deflate, StoredItemFile.Exception, stream, DateTime.UtcNow, string.Empty);

                                // Store the report
                                stream.SetLength(0);

                                try
                                {
                                    serializer = report.CustomInfo != null
                                                                             ? new XmlSerializer(typeof(Report), new[] { report.CustomInfo.GetType() })
                                                                             : new XmlSerializer(typeof(Report));

                                    serializer.Serialize(stream, report);
                                }
                                catch (Exception exception)
                                {
                                    Logger.Error(
                                        string.Format(
                                            "The given custom info of type [{0}] cannot be serialized. Make sure that given type and inner types are XML serializable.",
                                            report.CustomInfo.GetType()),
                                        exception);
                                    report.CustomInfo = null;
                                    serializer        = new XmlSerializer(typeof(Report));
                                    serializer.Serialize(stream, report);
                                }

                                stream.Position = 0;
                                zipStorer.AddStream(ZipStorer.Compression.Deflate, StoredItemFile.Report, stream, DateTime.UtcNow, string.Empty);

                                // Add the memory minidump to the report file (only if configured so)
                                if (DumpWriter.Write(minidumpFilePath))
                                {
                                    zipStorer.AddFile(ZipStorer.Compression.Deflate, minidumpFilePath, StoredItemFile.MiniDump, string.Empty);
                                    File.Delete(minidumpFilePath);
                                }

                                // Add any user supplied files in the report (if any)
                                if (Settings.AdditionalReportFiles.Count != 0)
                                {
                                    // ToDo: This needs a lot more work!
                                    this.AddAdditionalFiles(zipStorer);
                                }
                            }

                    Logger.Trace("Created a new report file. Currently the number of report files queued to be send is: " + Storer.GetReportCount());
                }
                else
                {
                    Logger.Trace(
                        "Current report count is at its limit as per 'Settings.MaxQueuedReports (" + Settings.MaxQueuedReports
                        + ")' setting: Skipping bug report generation.");
                }
            }
            else
            {
                Logger.Trace(
                    "As per setting 'Settings.StopReportingAfter(" + Settings.StopReportingAfter
                    + ")', bug reporting feature was enabled for a certain amount of time which has now expired: Bug reporting is now disabled.");

                // ToDo: Completely eliminate this with SettingsOverride.DisableReporting = true; since enumerating filesystem adds overhead);
                if (Storer.GetReportCount() > 0)
                {
                    Logger.Trace(
                        "As per setting 'Settings.StopReportingAfter(" + Settings.StopReportingAfter
                        + ")', bug reporting feature was enabled for a certain amount of time which has now expired: Truncating all expired bug reports.");
                    Storer.TruncateReportFiles(0);
                }
            }
        }