/// <summary> /// Creates a <see cref="Formatter{T}"/> for all the entries in the <see cref="LogEntry.ExtendedProperties"/> /// dictionary. /// </summary> private static Formatter <LogEntry> DictionaryTokenHandlerFactory(string parameter) { Dictionary <string, TokenHandler <KeyValuePair <string, object> > > handlers = new Dictionary <string, TokenHandler <KeyValuePair <string, object> > >(); handlers["newline"] = GenericTextFormatter <KeyValuePair <string, object> > .CreateSimpleTokenHandler(Environment.NewLine); handlers["tab"] = GenericTextFormatter <KeyValuePair <string, object> > .CreateSimpleTokenHandler("\t"); handlers["key"] = GenericTextFormatter <KeyValuePair <string, object> > .CreateSimpleTokenHandler(kvp => kvp.Key); handlers["value"] = GenericTextFormatter <KeyValuePair <string, object> > .CreateSimpleTokenHandler(kvp => kvp.Value.ToString()); GenericTextFormatter <KeyValuePair <string, object> > entryFormatter = new GenericTextFormatter <KeyValuePair <string, object> >(parameter, handlers); return(le => { StringBuilder entries = new StringBuilder(); foreach (var kvp in le.ExtendedProperties) { entryFormatter.Format(kvp, entries); } return entries.ToString(); }); }
/// <summary> /// Initializes a new instance of a <see cref="TextFormatter"> with a template and additional token handlers.</see> /// </summary> /// <param name="template">Template to be used when formatting.</param> /// <param name="extraTokenHandlers">The additional token handlers to use when processing the template.</param> protected TextFormatter(string template, IDictionary <string, TokenHandler <LogEntry> > extraTokenHandlers) { if (!string.IsNullOrEmpty(template)) { Template = template; } else { Template = DefaultTextFormat; } Dictionary <string, TokenHandler <LogEntry> > tokenHandlers = defaultTokenHandlers; if (extraTokenHandlers.Count > 0) { tokenHandlers = new Dictionary <string, TokenHandler <LogEntry> >(defaultTokenHandlers); foreach (var kvp in extraTokenHandlers) { tokenHandlers.Add(kvp.Key, kvp.Value); } } this.formatter = new GenericTextFormatter <LogEntry>(Template, tokenHandlers); }
/// <summary> /// Sets up the default token handlers for the <see cref="TextFormatter"/>; /// </summary> static TextFormatter() { defaultTokenHandlers = new Dictionary <string, TokenHandler <LogEntry> >(); emptyExtraTokenHandlers = new Dictionary <string, TokenHandler <LogEntry> >(); // constants defaultTokenHandlers["newline"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(Environment.NewLine); defaultTokenHandlers["tab"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler("\t"); // cached values defaultTokenHandlers["localMachine"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(LogEntryContext.GetMachineNameSafe()); defaultTokenHandlers["localProcessName"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(LogEntryContext.GetProcessNameSafe()); defaultTokenHandlers["localAppDomain"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(LogEntryContext.GetAppDomainNameSafe()); defaultTokenHandlers["localProcessId"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(LogEntryContext.GetProcessIdSafe()); // simple properties on log entry defaultTokenHandlers["applicationName"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.ApplicationName); defaultTokenHandlers["message"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.Message); defaultTokenHandlers["category"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => FormatCategoriesCollection(le.Categories)); defaultTokenHandlers["priority"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.Priority.ToString(CultureInfo.CurrentCulture)); defaultTokenHandlers["eventid"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.EventId.ToString(CultureInfo.CurrentCulture)); defaultTokenHandlers["severity"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => FormatSeverity(le.Severity)); defaultTokenHandlers["title"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.Title); defaultTokenHandlers["errorMessages"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.ErrorMessages); defaultTokenHandlers["machine"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.MachineName); defaultTokenHandlers["appDomain"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.AppDomainName); defaultTokenHandlers["processId"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.ProcessId); defaultTokenHandlers["processName"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.ProcessName); defaultTokenHandlers["threadName"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.ManagedThreadName); defaultTokenHandlers["win32ThreadId"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.Win32ThreadId); defaultTokenHandlers["activity"] = GenericTextFormatter <LogEntry> .CreateSimpleTokenHandler(le => le.ActivityId.ToString("D", CultureInfo.CurrentCulture)); // parameterized tokens defaultTokenHandlers["timestamp"] = GenericTextFormatter <LogEntry> .CreateParameterizedTokenHandler(TimestampFormatterFactory); defaultTokenHandlers["property"] = GenericTextFormatter <LogEntry> .CreateParameterizedTokenHandler(ReflectedPropertyFormatterFactory); defaultTokenHandlers["keyvalue"] = GenericTextFormatter <LogEntry> .CreateParameterizedTokenHandler(KeyValueFormatterFactory); defaultTokenHandlers["dictionary"] = GenericTextFormatter <LogEntry> .CreateParameterizedTokenHandler(DictionaryTokenHandlerFactory); }