static LogService() { var formatter = new TextFormatter ("Timestamp: {timestamp}{newline}" + "Message: {message}{newline}" + "Category: {category}{newline}"); //Create the Trace listeners var logFileListener = new FlatFileTraceListener(@"C:\temp\temp.log", "", "", formatter); //Add the trace listeners to the source var mainLogSource = new LogSource("MainLogSource", SourceLevels.All); mainLogSource.Listeners.Add(logFileListener); var nonExistantLogSource = new LogSource("Empty"); IDictionary<string, LogSource> traceSources = new Dictionary<string, LogSource> {{"Info", mainLogSource}, {"Warning", mainLogSource}, {"Error", mainLogSource}}; Writer = new LogWriterImpl(new ILogFilter[0], traceSources, nonExistantLogSource, nonExistantLogSource, mainLogSource, "Info", false, true); }
public LoggingViewModel() { var logFormatter = new TextFormatter(); var flatFileTraceListener = new FlatFileTraceListener("log.hblog", "-------------------------", "-------------------------", logFormatter); var config = new LoggingConfiguration(); config.AddLogSource("Hummingbird", SourceLevels.All, true).AddTraceListener(flatFileTraceListener); Logger = new LogWriter(config); }
static void Main(string[] args) { TextFormatter briefFormatter = new TextFormatter(); var flatFileTraceListerner = new FlatFileTraceListener(@"C:\temp\xxx.log", "-------------------------------", "-------------------------------", briefFormatter); var config = new LoggingConfiguration(); config.AddLogSource("my_log", System.Diagnostics.SourceLevels.All, true).AddTraceListener(flatFileTraceListerner); LogWriter logger = new LogWriter(config); }
private static LoggingConfiguration BuildProgrammaticConfig() { var formatter = new TextFormatter(); var flatFileTraceListener = new FlatFileTraceListener( "log.txt", "----------------------------------------", "----------------------------------------", formatter); var config = new LoggingConfiguration(); config.AddLogSource("General", SourceLevels.All, true).AddTraceListener(flatFileTraceListener); config.IsTracingEnabled = true; return config; }
private void ConfigureLogging() { // Formatter TextFormatter fm = new TextFormatter(); var flatFileTraceListener = new FlatFileTraceListener(@"trace.log", "----------------------------------------", "----------------------------------------", fm); // Build Configuration var config = new LoggingConfiguration(); config.AddLogSource("General", SourceLevels.All, true).AddTraceListener(flatFileTraceListener); config.IsTracingEnabled = true; config.SpecialSources.AllEvents.AddTraceListener(flatFileTraceListener); Logger.SetLogWriter(new LogWriter(config)); }
/// <summary> /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. /// Builds a <see cref="FlatFileTraceListener"/> based on an instance of <see cref="FlatFileTraceListenerData"/>. /// </summary> /// <seealso cref="TraceListenerCustomFactory"/> /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param> /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="FlatFileTraceListenerData"/>.</param> /// <param name="configurationSource">The source for configuration objects.</param> /// <param name="reflectionCache">The cache to use retrieving reflection information.</param> /// <returns>A fully initialized instance of <see cref="FlatFileTraceListener"/>.</returns> public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { FlatFileTraceListenerData castedObjectConfiguration = (FlatFileTraceListenerData)objectConfiguration; ILogFormatter formatter = GetFormatter(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache); TraceListener createdObject = new FlatFileTraceListener( castedObjectConfiguration.FileName, castedObjectConfiguration.Header, castedObjectConfiguration.Footer, formatter); return createdObject; }
/// <summary> /// Static constructor /// </summary> static Logger() { string LogFile = ConfigurationManager.AppSettings["LogFile"].ToString();// a static constructor works because changing the web.config restarts the appliaction. // this defaults to the namespace WebForms even when used in another application so it's not used //string LogFile=Properties.Settings.Default.LogFile; if(ConfigurationManager.AppSettings["LogErr"].ToString().ToLower()=="yes") { LogErr=true; } if(ConfigurationManager.AppSettings["LogInfo"].ToString().ToLower()=="yes") { LogInfo=true; } // formatter TextFormatter formatter = new TextFormatter("[{timestamp(local)}] [{machine}] {category} \t: {message}"); // listeners FlatFileTraceListener logFileListener=new FlatFileTraceListener(LogFile,"","",formatter); RollingFlatFileTraceListener rollingFlatFileListener=new RollingFlatFileTraceListener(LogFile,"","",formatter,1000,"yyyy-MM-dd",RollFileExistsBehavior.Increment,RollInterval.Day); //uncomment if an event log is needed //FormattedEventLogTraceListener logEventListener = new FormattedEventLogTraceListener("Enterprise Library Logging",formatter); // Sources LogSource mainLogSource = new LogSource("MainLogSource",SourceLevels.All); //mainLogSource.Listeners.Add(logFileListener);//regular flat file mainLogSource.Listeners.Add(rollingFlatFileListener); //uncomment if an event log is needed //LogSource errorLogSource = new LogSource("ErrorLogSource",SourceLevels.Error); //errorLogSource.Listeners.Add(logEventListener); // empty source LogSource nonExistantLogSource = new LogSource("Empty");//non matching category. // trace sources IDictionary<string,LogSource> traceSources = new Dictionary<string,LogSource>(); //traceSources.Add("Error",errorLogSource);//uncomment if an event log is needed traceSources.Add("Warning",mainLogSource); traceSources.Add("Information",mainLogSource); // log writer writer = new LogWriter(new ILogFilter[0],traceSources,mainLogSource,nonExistantLogSource, mainLogSource,"Error",false,true); //writer = new LogWriter(new ILogFilter[0],traceSources,mainLogSource,nonExistantLogSource, //errorLogSource,"Error",false,true);//uncomment if 'internal' error are to be logged to an event log is needed }
public static void InitializeLogManager() { if (formatter == null) { formatter = new TextFormatter() { Template = " ------------------------------------------------------{newline}Timestamp:{timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}Title:{title}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}-----------------------------------------)}", }; } if (flatFileTraceListener == null) { flatFileTraceListener = new FlatFileTraceListener(formatter) { }; } if (logSource == null) { logSource = new LogSource("Logging", new List<TraceListener>() { flatFileTraceListener }, SourceLevels.Information); } }
internal static LoggingConfiguration BuildLoggingConfig(GlobalStateModel model) { // Formatters //TextFormatter formatter = new TextFormatter("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title:{title}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"); TextFormatter errorFormatter = new TextFormatter("{timestamp} {message}"); TextFormatter consoleFormatter = new TextFormatter("{timestamp} {severity} : {message}"); // Listeners var errorFlatFileTraceListener = new FlatFileTraceListener(Directory.GetCurrentDirectory() + "\\" + "Error.log", "----------------------------------------", "----------------------------------------", errorFormatter); var outputFlatFileTraceListener = new FlatFileTraceListener(Directory.GetCurrentDirectory() + "\\" + "Output.log", "----------------------------------------", "----------------------------------------", errorFormatter); var outputViewTraceListener = new OutputViewTraceListener(model, consoleFormatter); //var consoleTraceListener = new CustomTraceListener(); // Build Configuration var config = new LoggingConfiguration(); config.AddLogSource("Error", SourceLevels.All, true).AddTraceListener(errorFlatFileTraceListener); config.AddLogSource("Output", SourceLevels.All, true).AddTraceListener(outputViewTraceListener); config.LogSources["Output"].AddTraceListener(outputFlatFileTraceListener); config.AddLogSource("Debug", SourceLevels.All, true).AddTraceListener(outputViewTraceListener); config.AddLogSource("OutputToFile", SourceLevels.All, true).AddTraceListener(outputFlatFileTraceListener); return config; }
public static LoggingConfiguration BuildProgrammaticConfig() { // Formatter TextFormatter briefFormatter = new TextFormatter("Timestamp: {timestamp(local)}{newline}Message: {message}{newline}"); // Trace Listener var flatFileTraceListener = new FlatFileTraceListener( @"C:\Temp\jarwin.log", "----------------------------------------", "----------------------------------------", briefFormatter); // Build Configuration var config = new LoggingConfiguration(); config.AddLogSource("jarwin", SourceLevels.All, true) .AddTraceListener(flatFileTraceListener); config.IsLoggingEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings["loggingEnabled"]); return config; }
private LoggingConfiguration BuildProgrammaticConfig() { // Formatters TextFormatter briefFormatter = new TextFormatter("Timestamp: {timestamp(local)}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}ActivityId: {property(ActivityId)}{newline}Severity: {severity}{newline}Title:{title}{newline}"); TextFormatter extendedFormatter = new TextFormatter("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title: {title}{newline}Activity ID: {property(ActivityId)}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"); // Category Filters ICollection<string> categories = new List<string>(); categories.Add("BlockedByFilter"); // Log Filters var priorityFilter = new PriorityFilter("Priority Filter", 2, 99); var logEnabledFilter = new LogEnabledFilter("LogEnabled Filter", true); var categoryFilter = new CategoryFilter("Category Filter", categories, CategoryFilterMode.AllowAllExceptDenied); // Trace Listeners //var causeLoggingErrorTraceListener = new FormattedDatabaseTraceListener(DatabaseFactory.CreateDatabase("DoesNotExist"), "WriteLog", "AddCategory", null); //var databaseTraceListener = new FormattedDatabaseTraceListener(DatabaseFactory.CreateDatabase("ExampleDatabase"), "WriteLog", "AddCategory", extendedFormatter); var flatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\FlatFile.log", "----------------------------------------", "----------------------------------------", briefFormatter); var eventLog = new EventLog("Application", ".", "Enterprise Library Logging"); var eventLogTraceListener = new FormattedEventLogTraceListener(eventLog); var rollingFlatFileTraceListener = new RollingFlatFileTraceListener(@"C:\Temp\RollingFlatFile.log", "----------------------------------------", "----------------------------------------", extendedFormatter, 20, "yyyy-MM-dd", RollFileExistsBehavior.Increment, RollInterval.None, 3); var unprocessedFlatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\Unprocessed.log", "----------------------------------------", "----------------------------------------", extendedFormatter); var xmlTraceListener = new XmlTraceListener(@"C:\Temp\XmlLogFile.xml"); xmlTraceListener.Filter = new EventTypeFilter(SourceLevels.Error); // Build Configuration var config = new LoggingConfiguration(); config.Filters.Add(priorityFilter); config.Filters.Add(logEnabledFilter); config.Filters.Add(categoryFilter); config.AddLogSource("BlockedByFilter", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); //config.AddLogSource("CauseLoggingError", SourceLevels.All, true).AddTraceListener(causeLoggingErrorTraceListener); //config.AddLogSource("Database", SourceLevels.All, true).AddTraceListener(databaseTraceListener); // The defaults for the asynchronous wrapper are: // bufferSize: 30000 // disposeTimeout: infinite //config.AddLogSource("AsyncDatabase", SourceLevels.All, true).AddAsynchronousTraceListener(databaseTraceListener); config.AddLogSource("DiskFiles", SourceLevels.All, true).AddTraceListener(flatFileTraceListener); config.AddLogSource("General", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); config.AddLogSource("Important", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); config.LogSources["Important"].AddTraceListener(rollingFlatFileTraceListener); // Special Sources Configuration config.SpecialSources.Unprocessed.AddTraceListener(unprocessedFlatFileTraceListener); config.SpecialSources.LoggingErrorsAndWarnings.AddTraceListener(eventLogTraceListener); return config; }
static LoggingConfiguration BuildProgrammaticConfig() { // Formatters TextFormatter formatter = new TextFormatter("Timestamp: {timestamp(local)}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}ActivityId: {property(ActivityId)}{newline}Severity: {severity}{newline}Title:{title}{newline}"); // Category Filters ICollection<string> categories = new List<string>(); categories.Add("BlockedByFilter"); // Log Filters var priorityFilter = new PriorityFilter("Priority Filter", 2, 99); var logEnabledFilter = new LogEnabledFilter("LogEnabled Filter", true); var categoryFilter = new CategoryFilter("Category Filter", categories, CategoryFilterMode.AllowAllExceptDenied); // Trace Listeners var flatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\ConfigSampleFlatFile.log", "----------------------------------------", "----------------------------------------", formatter); // Build Configuration var config = new LoggingConfiguration(); config.Filters.Add(priorityFilter); config.Filters.Add(logEnabledFilter); config.Filters.Add(categoryFilter); config.AddLogSource("General", SourceLevels.All, true, flatFileTraceListener); return config; }
private static LoggingConfiguration BuildLoggingConfig() { // Formatters TextFormatter formatter = new TextFormatter("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title:{title}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"); // Listeners var flatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\SalaryCalculator.log", "----------------------------------------", "----------------------------------------", formatter); var eventLog = new EventLog("Application", ".", "Enterprise Library Logging"); var eventLogTraceListener = new FormattedEventLogTraceListener(eventLog); // Build Configuration var config = new LoggingConfiguration(); config.AddLogSource("General", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); config.LogSources["General"].AddTraceListener(flatFileTraceListener); // Special Sources Configuration config.SpecialSources.LoggingErrorsAndWarnings.AddTraceListener(eventLogTraceListener); return config; }