private void WriteReport(ZipStorer zipStorer, Report report) { using (var stream = new MemoryStream()) { // Store the report as XML file try { var serializer = report.CustomInfo != null ? new XmlSerializer(typeof(Report), new[] { report.CustomInfo.GetType() }) : new XmlSerializer(typeof(Report)); serializer.Serialize(stream, report); } catch (Exception exception) { if (report.CustomInfo != null) { 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; var serializer = new XmlSerializer(typeof(Report)); serializer.Serialize(stream, report); } stream.Position = 0; zipStorer.AddStream(ZipStorer.Compression.Deflate, StoredItemFile.Report, stream, DateTime.UtcNow, string.Empty); } }
private void AddToZip(ZipStorer zipStorer, string basePath, string path) { if (basePath == null) return; path = Path.GetFullPath(path); // If this is not inside basePath, lets change the basePath so at least some directories are kept if (!path.StartsWith(basePath)) { basePath = Path.GetDirectoryName(path); if (basePath == null) return; } if (Directory.Exists(path)) { foreach (var file in Directory.GetFiles(path)) AddToZip(zipStorer, basePath, file); foreach (var dir in Directory.GetDirectories(path)) AddToZip(zipStorer, basePath, dir); } else if (File.Exists(path)) { var nameInZip = path.Substring(basePath.Length); if (nameInZip.StartsWith("\\") || nameInZip.StartsWith("/")) nameInZip = nameInZip.Substring(1); nameInZip = Path.Combine("files", nameInZip); zipStorer.AddFile(ZipStorer.Compression.Deflate, path, nameInZip, string.Empty); } }
private void WriteException(ZipStorer zipStorer, SerializableException serializableException) { using (var stream = new MemoryStream()) { // Store the exception as separate file 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); } }
private void AddAdditionalFiles(ZipStorer zipStorer) { foreach (var mask in _settings.AdditionalReportFiles) { // Join before spliting because the mask may have some folders inside it var dir = Path.GetDirectoryName(mask); if (string.IsNullOrEmpty(dir)) { Logger.Warn("Skipped non-absolute mask " + mask); } var file = Path.GetFileName(mask); if (dir == null || !Directory.Exists(dir) || file == null) continue; if (file.Contains("*") || file.Contains("?")) { foreach (var item in Directory.GetFiles(dir, file)) { AddToZip(zipStorer, dir, item); } } else { AddToZip(zipStorer, dir, mask); } } }