public void ShouldNotReplaceWithoutReplacement() { string template = "No replacements here"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); Assert.AreEqual(template, formatter.FormatCategory(template)); }
private TraceLogEntry GetLogEntry(IMethodInvocation input) { TraceLogEntry logEntry = new TraceLogEntry(); CategoryFormatter formatter = new CategoryFormatter(input.MethodBase); foreach (string category in this.categories) { logEntry.Categories.Add(formatter.FormatCategory(category)); } logEntry.EventId = this.eventId; logEntry.Priority = this.priority; logEntry.Severity = this.severity; logEntry.Title = LogCallHandlerDefaults.Title; if (this.includeParameters) { Dictionary <string, object> parameters = new Dictionary <string, object>(); for (int i = 0; i < input.Arguments.Count; i++) { parameters[input.Arguments.GetParameterInfo(i).Name] = input.Arguments[i]; } logEntry.ExtendedProperties = parameters; } if (this.includeCallStack) { logEntry.CallStack = Environment.StackTrace; } logEntry.TypeName = input.Target.GetType().FullName; logEntry.MethodName = input.MethodBase.Name; return(logEntry); }
public void ShouldPropertyEscapeBackslashes() { string template = @"Here's a method: \\{method}"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); string formatted = formatter.FormatCategory(template); Assert.AreEqual(@"Here's a method: \TargetMethod", formatted); }
public void ShouldProperlyEscapeOpenBraces() { string template = @"Not a \{namespace}"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); string formatted = formatter.FormatCategory(template); Assert.AreEqual("Not a {namespace}", formatted); }
public void ShouldReplaceAssembly() { string template = "Assembly {assembly}"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); string formatted = formatter.FormatCategory(template); Assert.IsTrue(formatted.StartsWith("Assembly Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Tests")); }
public void ShouldReplaceTypeName() { string template = "Type {type}"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); Assert.AreEqual("Type CategoryFormatterFixture", formatter.FormatCategory(template)); }
public void ShouldReplaceMethodName() { string template = "Method {method}"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); Assert.AreEqual("Method TargetMethod", formatter.FormatCategory(template)); }
public void ShouldReplaceNamespace() { string template = "Namespace {namespace}"; CategoryFormatter formatter = new CategoryFormatter(GetTargetMethod()); string formatted = formatter.FormatCategory(template); Assert.AreEqual("Namespace Microsoft.Practices.EnterpriseLibrary.Logging.Tests.PolicyInjection", formatted); }
public void PopulateLogEntryForCallHandler(IMethodInvocation input, //List<string> categories, //bool includeParameters, bool includeCallStack, IMethodReturn result, long? tracingStartTicks, long? tracingEndTicks, long elapsedMilliseconds, string messageFormat, string moduleId, string functionId, ComponentType component) { MonitoringLogEntry logEntry = PopulateDefaultLogEntry(); using (BackgroundWorker worker = new BackgroundWorker()) { worker.DoWork += delegate(object oDoWork, DoWorkEventArgs eDoWork) { try { AppContext.SetToCallContext(AppContext.Current.ToDictionary()); CategoryFormatter formatter = new CategoryFormatter(input.MethodBase); logEntry = eDoWork.Argument as MonitoringLogEntry; //foreach (string category in categories) //{ // logEntry.Categories.Add(formatter.FormatCategory(category)); //} logEntry.Categories.Add(formatter.FormatCategory(LoggingCategories.Monitoring)); logEntry.Component = component; if (!Logger.ShouldLog(logEntry)) { return; } //When do the instrumentation log , we should log the parameter values information, otherwise not if ((logEntry.Flag & FilterFlag.InstrumentationFlag) == FilterFlag.InstrumentationFlag) { //if (includeParameters) { logEntry.ParameterValues = GenerateValuesInfo(input.Arguments, true); } } //if (includeCallStack) //{ // logEntry.CallStack = Environment.StackTrace; //} logEntry.TypeName = input.Target.GetType().FullName; logEntry.MethodName = string.Concat(input.MethodBase.DeclaringType.FullName, ".", input.MethodBase.Name); if (result == null) { PopulatePreLogEntry(logEntry); } else { PopulatePostLogEntry(logEntry); if ((logEntry.Flag & FilterFlag.InstrumentationFlag) == FilterFlag.InstrumentationFlag) { int depth = 0; object returnedValue = result.ReturnValue; logEntry.ReturnValue = GenerateValueInfo("ReturnValue", returnedValue, (returnedValue == null) ? null : returnedValue.GetType(), ref depth); } } logEntry.TracingStartTicks = tracingStartTicks; logEntry.TracingEndTicks = tracingEndTicks; logEntry.SecondsElapsed = GetSecondsElapsed(elapsedMilliseconds); logEntry.Message = string.Format(Resources.Culture, messageFormat, logEntry.ExtendedActivityId, logEntry.MethodName, logEntry.TracingEndTicks, logEntry.SecondsElapsed); logEntry.FunctionId = functionId; logEntry.ModuleId = moduleId; //if (result.ReturnValue != null && includeParameters) //{ // logEntry.ReturnValue = result.ReturnValue.ToString(); //} if (result != null && result.Exception != null) { logEntry.Exception = result.Exception.ToString(); } LogWriter logWriter = Logger.Writer; logWriter.Write(logEntry); } catch (Exception ex) { //Swallow exception, because it is logging should not impact the business function HiiPLoggingExceptionHandler handler = new HiiPLoggingExceptionHandler("Server Exception", 100, TraceEventType.Error, "Instrumtation logging of RowUpdated failed", 0, typeof(HiiPLoggingExceptionFormatter), ComponentType.StoredProcedure, Logger.Writer); handler.HandleException(ex, Guid.NewGuid()); } }; worker.RunWorkerAsync(logEntry); } }