public virtual void WriteException(Exception ex, IMemberDefinition member) { string[] exceptionLines = exceptionFormatter.Format(ex, member.FullName, member.DeclaringType.Module.FilePath); if (this.Settings.WriteExceptionsAsComments) { var commentedExceptionLines = exceptionLines.Select(exceptionLine => Language.CommentLineSymbol + " " + exceptionLine).ToArray(); formatter.WriteException(commentedExceptionLines); var commentedMailToMessage = Language.CommentLineSymbol + " " + #if !ENGINEONLYBUILD && !JUSTASSEMBLY JustDecompile.Settings.Utilities.MailToMessage; #else "mailto: [email protected]"; #endif formatter.WriteExceptionMailToLink(commentedMailToMessage, exceptionLines); } else { formatter.WriteException(exceptionLines); #if !ENGINEONLYBUILD && !JUSTASSEMBLY formatter.WriteExceptionMailToLink(JustDecompile.Settings.Utilities.MailToMessage, exceptionLines); #else formatter.WriteExceptionMailToLink("mailto: [email protected]", exceptionLines); #endif } }
public static string Format(this LoggerExtension extension, Exception exception) { Type type = exception.GetType(); IExceptionFormatter formatter = Logger.ExceptionFormatters.ContainsKey(type) ? Logger.ExceptionFormatters[type] : Logger.ExceptionFormatters[typeof(Exception)]; return(formatter.Format(exception)); }
public string Format(ILogEntry entry) { if (entry == null) { throw new ArgumentNullException(nameof(entry)); } var exceptionString = string.Empty; if (entry.Exception != null) { exceptionString = $@" {_exceptionFormatter.Format(entry.Exception)}"; } return ($@"DateTime (UTC): {entry.DateTimeUtc.ToString("MMM dd, HH:mm:ss")} EventId: {entry.EventId.ToString()} Severity: {entry.Severity.ToString()} Machine: {entry.MachineName} User: {entry.User} AppVersion: {entry.Version.ToString()} ---- Data ---- {entry.Data?.ToString() ?? "[NULL]"}{exceptionString} =========================================================================== "); }
public void GivenLogEntry_WithoutException_FormatsCorrectly() { ConfigureExceptionStringFormatter_ToReturnSomethingValid(); var input = new LogEntry(ConfigureSystemClockAmbientContext()) { Data = "The log data.", EventId = 555, Severity = Severity.Warning }; var actual = _sut.Format(input); Assert.That(string.IsNullOrEmpty(actual), Is.False); Assert.That(actual.Contains(input.Data.ToString()), Is.True); Assert.That(actual.Contains(input.EventId.ToString()), Is.True); Assert.That(actual.Contains(input.Severity.ToString()), Is.True); A.CallTo(() => _exceptionFormatter.Format(A <Exception> ._)) .MustNotHaveHappened(); }
public void Publish(Exception exception, string errorCode) { var eventLog = new EventLog { Source = "Application" }; var errorMessage = ExceptionFormatter.Format(exception, errorCode); eventLog.WriteEntry(errorMessage, EventLogEntryType.Error); }
public void Publish(Exception exception, string errorCode) { var dumpDirectory = CommonLogic.SafeMapPath("~/images/errors"); if (!Directory.Exists(dumpDirectory)) { Directory.CreateDirectory(dumpDirectory); } var filename = string.Format("{0}_{1}.txt", DateTime.Now.ToString("MM-dd-yyy_hhmmss"), errorCode); var filePath = CommonLogic.SafeMapPath(Path.Combine(dumpDirectory, filename)); var errorMessage = ExceptionFormatter.Format(exception, errorCode); File.WriteAllText(filePath, errorMessage); }
public void Publish(Exception exception, string errorCode) { var toAddress = AppLogic.AppConfig("MailMe_ErrorToAddress"); var toName = AppLogic.AppConfig("MailMe_ErrorToName"); var fromAddress = AppLogic.AppConfig("MailMe_ErrorFromAddress"); if (String.IsNullOrWhiteSpace(toAddress)) { toAddress = AppLogic.AppConfig("MailMe_ToAddress"); } if (String.IsNullOrWhiteSpace(toName)) { toName = AppLogic.AppConfig("MailMe_ToName"); } if (String.IsNullOrWhiteSpace(fromAddress)) { fromAddress = AppLogic.AppConfig("MailMe_FromAddress"); } if (String.IsNullOrWhiteSpace(fromAddress)) { fromAddress = AppLogic.AppConfig("MailMe_FromName"); } var errorMessage = ExceptionFormatter.Format(exception, errorCode); AppLogic.SendMail( subject: string.Format("Error on Site {0} (Error Code:{1})", AppLogic.AppConfig("StoreName"), errorCode), body: errorMessage, useHtml: false, fromAddress: fromAddress, fromName: toName, toAddress: toAddress, toName: toName, bccAddresses: String.Empty, replyToAddress: fromAddress); }
public bool WriteTypeToFile(TypeDefinition type, IProjectItemFileWriter itemWriter, Dictionary <string, ICollection <string> > membersToSkip, bool shouldBePartial, ILanguage language, out List <WritingInfo> writingInfos, out string theCodeString) { theCodeString = string.Empty; writingInfos = null; StringWriter theWriter = new StringWriter(); bool showCompilerGeneratedMembers = Utilities.IsVbInternalTypeWithoutRootNamespace(type) || Utilities.IsVbInternalTypeWithRootNamespace(type); IFormatter formatter = GetFormatter(theWriter); IWriterSettings settings = new WriterSettings(writeExceptionsAsComments: true, writeFullyQualifiedNames: decompilationPreferences.WriteFullNames, writeDocumentation: decompilationPreferences.WriteDocumentation, showCompilerGeneratedMembers: showCompilerGeneratedMembers, writeLargeNumbersInHex: decompilationPreferences.WriteLargeNumbersInHex); ILanguageWriter writer = language.GetWriter(formatter, this.exceptionFormater, settings); IWriterContextService writerContextService = this.GetWriterContextService(); writer.ExceptionThrown += OnExceptionThrown; writerContextService.ExceptionThrown += OnExceptionThrown; bool exceptionOccurred = false; try { if (!(writer is INamespaceLanguageWriter)) { writingInfos = writer.Write(type, writerContextService); } else { if (shouldBePartial) { writingInfos = (writer as INamespaceLanguageWriter).WritePartialTypeAndNamespaces(type, writerContextService, membersToSkip); } else { writingInfos = (writer as INamespaceLanguageWriter).WriteTypeAndNamespaces(type, writerContextService); } } this.RecordGeneratedFileData(type, itemWriter.FullSourceFilePath, theWriter, formatter, writerContextService, writingInfos); MemoryStream sourceFileStream = new MemoryStream(Encoding.UTF8.GetBytes(theWriter.ToString())); itemWriter.CreateProjectSourceFile(sourceFileStream); sourceFileStream.Close(); theWriter.Close(); } catch (Exception e) { exceptionOccurred = true; string[] exceptionMessageLines = exceptionFormater.Format(e, type.FullName, itemWriter.FullSourceFilePath); string exceptionMessage = string.Join(Environment.NewLine, exceptionMessageLines); string commentedExceptionMessage = language.CommentLines(exceptionMessage); itemWriter.CreateProjectSourceFile(new MemoryStream(Encoding.UTF8.GetBytes(commentedExceptionMessage))); OnExceptionThrown(this, e); } theCodeString = theWriter.ToString(); writer.ExceptionThrown -= OnExceptionThrown; writerContextService.ExceptionThrown -= OnExceptionThrown; return(exceptionOccurred || writerContextService.ExceptionsWhileDecompiling.Any()); }
public override Task <DecompileTypeResponse> DecompileType(DecompileTypeRequest request, ServerCallContext context) { if (!this.decompilationContext.DecompilationContext.FilePathToType.ContainsKey(request.FilePath)) { throw new RpcException(new Status(StatusCode.NotFound, "No type to corresponding file path")); } TypeDefinition type = this.decompilationContext.DecompilationContext.FilePathToType[request.FilePath]; IExceptionFormatter exceptionFormatter = SimpleExceptionFormatter.Instance; ILanguage language = LanguageFactory.GetLanguage(CSharpVersion.V7); StringWriter theWriter = new StringWriter(); CodeFormatter formatter = new CodeFormatter(theWriter); IWriterSettings settings = new WriterSettings(writeExceptionsAsComments: true, writeFullyQualifiedNames: false, writeDocumentation: true, showCompilerGeneratedMembers: false, writeLargeNumbersInHex: false); ILanguageWriter writer = language.GetWriter(formatter, exceptionFormatter, settings); IWriterContextService writerContextService = new TypeCollisionWriterContextService(new ProjectGenerationDecompilationCacheService(), true); try { List <WritingInfo> infos = (writer as INamespaceLanguageWriter).WriteTypeAndNamespaces(type, writerContextService); DecompiledTypeMetadata decompiledTypeMetadata = new DecompiledTypeMetadata(); decompiledTypeMetadata.CodeSpanToMemberReference.AddRange(formatter.CodeSpanToMemberReference); foreach (WritingInfo info in infos) { decompiledTypeMetadata.MemberDeclarationToCodeSpan.AddRange(info.MemberDeclarationToCodeSpan); decompiledTypeMetadata.CodeMappingInfo.NodeToCodeMap.AddRange(info.CodeMappingInfo.NodeToCodeMap); decompiledTypeMetadata.CodeMappingInfo.InstructionToCodeMap.AddRange(info.CodeMappingInfo.InstructionToCodeMap); decompiledTypeMetadata.CodeMappingInfo.FieldConstantValueToCodeMap.AddRange(info.CodeMappingInfo.FieldConstantValueToCodeMap); decompiledTypeMetadata.CodeMappingInfo.VariableToCodeMap.AddRange(info.CodeMappingInfo.VariableToCodeMap); decompiledTypeMetadata.CodeMappingInfo.ParameterToCodeMap.AddRange(info.CodeMappingInfo.ParameterToCodeMap); decompiledTypeMetadata.CodeMappingInfo.MethodDefinitionToMethodReturnTypeCodeMap.AddRange(info.CodeMappingInfo.MethodDefinitionToMethodReturnTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.FieldDefinitionToFieldTypeCodeMap.AddRange(info.CodeMappingInfo.FieldDefinitionToFieldTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.PropertyDefinitionToPropertyTypeCodeMap.AddRange(info.CodeMappingInfo.PropertyDefinitionToPropertyTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.EventDefinitionToEventTypeCodeMap.AddRange(info.CodeMappingInfo.EventDefinitionToEventTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.ParameterDefinitionToParameterTypeCodeMap.AddRange(info.CodeMappingInfo.ParameterDefinitionToParameterTypeCodeMap); decompiledTypeMetadata.CodeMappingInfo.VariableDefinitionToVariableTypeCodeMap.AddRange(info.CodeMappingInfo.VariableDefinitionToVariableTypeCodeMap); } this.decompilationContext.AddTypeMetadataToCache(type, decompiledTypeMetadata); return(Task.FromResult(new DecompileTypeResponse() { SourceCode = theWriter.ToString() })); } catch (Exception e) { string[] exceptionMessageLines = exceptionFormatter.Format(e, type.FullName, null); string exceptionMessage = string.Join(Environment.NewLine, exceptionMessageLines); string commentedExceptionMessage = language.CommentLines(exceptionMessage); return(Task.FromResult(new DecompileTypeResponse() { SourceCode = commentedExceptionMessage })); } }