Class LogWriter
- Namespace
- Microsoft.Practices.EnterpriseLibrary.Logging
- Assembly
- Microsoft.Practices.EnterpriseLibrary.Logging.dll
Instance based class to write log messages based on a given configuration. Messages are routed based on category.
public class LogWriter : ILogFilterErrorHandler, IDisposable
- Inheritance
-
LogWriter
- Implements
- Inherited Members
Remarks
To write log messages to the default configuration, use the Logger facade.
The LogWriter works as an entry point to the System.Diagnostics trace listeners. It will trace the LogEntry through the Microsoft.Practices.EnterpriseLibrary.Logging.TraceListenerss associated with the LogSources for all the matching categories in the elements of the Categories property of the log entry. If the "all events" special log source is configured, the log entry will be traced through the log source regardles of other categories that might have matched. If the "all events" special log source is not configured and the "unprocessed categories" special log source is configured, and the category specified in the logEntry being logged is not defined, then the logEntry will be logged to the "unprocessed categories" special log source. If both the "all events" and "unprocessed categories" special log sources are not configured and the property LogWarningsWhenNoCategoriesMatch is set to true, then the logEntry is logged to the "logging errors and warnings" special log source.
Constructors
LogWriter(LogWriterStructureHolder)
Initializes a new instance of the LogWriter class with the specified logging stack.
public LogWriter(LogWriterStructureHolder structureHolder)
Parameters
structureHolder
LogWriterStructureHolderThe initial implementation of the logging stack
LogWriter(LoggingConfiguration)
Initializes a new instance of the LogWriter class with the specified configuration.
public LogWriter(LoggingConfiguration config)
Parameters
config
LoggingConfigurationThe configuration to use.
LogWriter(IEnumerable<ILogFilter>, IDictionary<string, LogSource>, LogSource, LogSource, LogSource, string, bool, bool)
Initializes a new instance of the LogWriter class with the specified filters, trace sources, default category, and tracing options.
public LogWriter(IEnumerable<ILogFilter> filters, IDictionary<string, LogSource> traceSources, LogSource allEventsTraceSource, LogSource notProcessedTraceSource, LogSource errorsTraceSource, string defaultCategory, bool tracingEnabled, bool logWarningsWhenNoCategoriesMatch)
Parameters
filters
IEnumerable<ILogFilter>The collection of filters to use when processing an entry.
traceSources
IDictionary<string, LogSource>The trace sources to dispatch entries to.
allEventsTraceSource
LogSourceThe special LogSource to which all log entries should be logged.
notProcessedTraceSource
LogSourceThe special LogSource to which log entries with at least one non-matching category should be logged.
errorsTraceSource
LogSourceThe special LogSource to which internal errors must be logged.
defaultCategory
stringThe default category to set when the entry categories list of a log entry is empty.
tracingEnabled
boolThe tracing status.
logWarningsWhenNoCategoriesMatch
booltrue to log warnings when a non-matching category is found; otherwise, false.
LogWriter(IEnumerable<ILogFilter>, IDictionary<string, LogSource>, LogSource, LogSource, LogSource, string, bool, bool, bool)
Initializes a new instance of the LogWriter class with the specified filters, trace sources, default category, tracing options, and impersonation option.
public LogWriter(IEnumerable<ILogFilter> filters, IDictionary<string, LogSource> traceSources, LogSource allEventsTraceSource, LogSource notProcessedTraceSource, LogSource errorsTraceSource, string defaultCategory, bool tracingEnabled, bool logWarningsWhenNoCategoriesMatch, bool revertImpersonation)
Parameters
filters
IEnumerable<ILogFilter>The collection of filters to use when processing an entry.
traceSources
IDictionary<string, LogSource>The trace sources to dispatch entries to.
allEventsTraceSource
LogSourceThe special LogSource to which all log entries should be logged.
notProcessedTraceSource
LogSourceThe special LogSource to which log entries with at least one non-matching category should be logged.
errorsTraceSource
LogSourceThe special LogSource to which internal errors must be logged.
defaultCategory
stringThe default category to set when the entry categories list of a log entry is empty.
tracingEnabled
boolThe tracing status.
logWarningsWhenNoCategoriesMatch
booltrue to log warnings when a non-matching category is found; otherwise, false.
revertImpersonation
booltrue to revert impersonation while logging; otherwise, false.
LogWriter(IEnumerable<ILogFilter>, IDictionary<string, LogSource>, LogSource, string)
Initializes a new instance of the LogWriter class with the specified filters, trace sources, and default category.
public LogWriter(IEnumerable<ILogFilter> filters, IDictionary<string, LogSource> traceSources, LogSource errorsTraceSource, string defaultCategory)
Parameters
filters
IEnumerable<ILogFilter>The collection of filters to use when processing an entry.
traceSources
IDictionary<string, LogSource>The trace sources to dispatch entries to.
errorsTraceSource
LogSourceThe special LogSource to which internal errors must be logged.
defaultCategory
stringThe default category to set when the entry categories list is empty.
LogWriter(IEnumerable<ILogFilter>, IEnumerable<LogSource>, LogSource, LogSource, LogSource, string, bool, bool)
Initializes a new instance of the LogWriter class with the specified filters, trace sources, default category, tracing options, and warning options.
public LogWriter(IEnumerable<ILogFilter> filters, IEnumerable<LogSource> traceSources, LogSource allEventsTraceSource, LogSource notProcessedTraceSource, LogSource errorsTraceSource, string defaultCategory, bool tracingEnabled, bool logWarningsWhenNoCategoriesMatch)
Parameters
filters
IEnumerable<ILogFilter>The collection of filters to use when processing an entry.
traceSources
IEnumerable<LogSource>The trace sources to dispatch entries to.
allEventsTraceSource
LogSourceThe special LogSource to which all log entries should be logged.
notProcessedTraceSource
LogSourceThe special LogSource to which log entries with at least one non-matching category should be logged.
errorsTraceSource
LogSourceThe special LogSource to which internal errors must be logged.
defaultCategory
stringThe default category to set when the entry categories list is empty.
tracingEnabled
boolThe tracing status.
logWarningsWhenNoCategoriesMatch
booltrue to log warnings when a non-matching category is found; otherwise, false.
LogWriter(IEnumerable<ILogFilter>, IEnumerable<LogSource>, LogSource, string)
Initializes a new instance of the LogWriter class with the specified filters, trace sources, and default category.
public LogWriter(IEnumerable<ILogFilter> filters, IEnumerable<LogSource> traceSources, LogSource errorsTraceSource, string defaultCategory)
Parameters
filters
IEnumerable<ILogFilter>The collection of filters to use when processing an entry.
traceSources
IEnumerable<LogSource>The trace sources to dispatch entries to.
errorsTraceSource
LogSourceThe special LogSource to which internal errors must be logged.
defaultCategory
stringThe default category to set when the entry categories list is empty.
Fields
LogWriterFailureEventID
EventID used on LogEntries that occur when internal LogWriter mechanisms fail.
public const int LogWriterFailureEventID = 6352
Field Value
Properties
TraceSources
public IDictionary<string, LogSource> TraceSources { get; }
Property Value
Methods
Configure(Action<LoggingConfiguration>)
Configures the LogWriter object.
public void Configure(Action<LoggingConfiguration> configurationScript)
Parameters
configurationScript
Action<LoggingConfiguration>An action that configures the log writer.
Remarks
Logging is delayed until the configuration changes are applied.
Dispose()
Releases the resources used by the LogWriter.
public void Dispose()
Dispose(bool)
Releases the resources used by the LogWriter.
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool">true to release managed and unmanaged resources; false to release only unmanaged resources.
FilterCheckingFailed(Exception, LogEntry, ILogFilter)
This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. Performs any action to handle an error during checking.
public bool FilterCheckingFailed(Exception ex, LogEntry logEntry, ILogFilter filter)
Parameters
ex
ExceptionThe exception that was raised during filter evaluation.
logEntry
LogEntryThe log entry that was evaluated.
filter
ILogFilterThe filter that raised the exception.
Returns
~LogWriter()
Finalizes an instance of the LogWriter class.
protected ~LogWriter()
FlushContextItems()
Empties the dictionary of context items.
[SecurityCritical]
public void FlushContextItems()
GetFilter(string)
Returns the filter named name
.
public ILogFilter GetFilter(string name)
Parameters
name
stringThe name of the filter required.
Returns
- ILogFilter
The filter named
name
in the filters collection, or null if there is no such filter.
GetFilter<T>()
Returns the filter of type T
.
public T GetFilter<T>() where T : class, ILogFilter
Returns
- T
The instance of
T
in the filters collection, or null if there is no such instance.
Type Parameters
T
The type of filter requiered.
GetFilter<T>(string)
Returns the filter of type T
named name
.
public T GetFilter<T>(string name) where T : class, ILogFilter
Parameters
name
stringThe name of the filter required.
Returns
- T
The instance of
T
namedname
in the filters collection, or null if there is no such instance.
Type Parameters
T
The type of filter required.
GetMatchingTraceSources(LogEntry)
Gets a list of LogSource objects for the log entry.
public IEnumerable<LogSource> GetMatchingTraceSources(LogEntry logEntry)
Parameters
Returns
- IEnumerable<LogSource>
A collection of LogSource objects.
IsLoggingEnabled()
Queries whether logging is enabled.
public bool IsLoggingEnabled()
Returns
- bool
true if logging is enabled.
IsTracingEnabled()
Queries whether tracing is enabled.
public bool IsTracingEnabled()
Returns
- bool
true if tracing is enabled.
SetContextItem(object, object)
Adds a key/value pair to the System.Runtime.Remoting.Messaging.CallContext dictionary.
Context items will be recorded with every log entry.
[SecurityCritical]
public void SetContextItem(object key, object value)
Parameters
Examples
The following example demonstrates use of the AddContextItem method.
Logger.SetContextItem("SessionID", myComponent.SessionId);
ShouldLog(LogEntry)
Queries whether the specified LogEntry should be logged.
public bool ShouldLog(LogEntry log)
Parameters
log
LogEntryThe log entry to check.
Returns
Write(LogEntry)
Writes the specified log entry.
public void Write(LogEntry log)
Parameters
log
LogEntryThe log entry to write.
Write(object)
Write a new log entry to the default category.
public void Write(object message)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
Write(object, IDictionary<string, object>)
Write a new log entry and a dictionary of extended properties.
public void Write(object message, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Write(object, IEnumerable<string>)
Write a new log entry to a specific collection of categories.
public void Write(object message, IEnumerable<string> categories)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
Write(object, IEnumerable<string>, IDictionary<string, object>)
Write a new log entry to a specific collection of categories with a dictionary of extended properties.
public void Write(object message, IEnumerable<string> categories, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Write(object, IEnumerable<string>, int)
Write a new log entry with a specific collection of categories and priority.
public void Write(object message, IEnumerable<string> categories, int priority)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
Write(object, IEnumerable<string>, int, IDictionary<string, object>)
Write a new log entry to with a specific collection of categories, priority and a dictionary of extended properties.
public void Write(object message, IEnumerable<string> categories, int priority, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Write(object, IEnumerable<string>, int, int)
Write a new log entry with a specific collection of categories, priority and event id.
public void Write(object message, IEnumerable<string> categories, int priority, int eventId)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
Write(object, IEnumerable<string>, int, int, TraceEventType)
Write a new log entry with a specific collection of categories, priority, event id and severity.
public void Write(object message, IEnumerable<string> categories, int priority, int eventId, TraceEventType severity)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
severity
TraceEventTypeLog entry severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).
Write(object, IEnumerable<string>, int, int, TraceEventType, string)
Write a new log entry with a specific collection of categories, priority, event id, severity and title.
public void Write(object message, IEnumerable<string> categories, int priority, int eventId, TraceEventType severity, string title)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
severity
TraceEventTypeLog message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).
title
stringAdditional description of the log entry message
Write(object, IEnumerable<string>, int, int, TraceEventType, string, IDictionary<string, object>)
Write a new log entry with a specific category, priority, event Id, severity title and dictionary of extended properties.
public void Write(object message, IEnumerable<string> categories, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
categories
IEnumerable<string>Category names used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
severity
TraceEventTypeLog message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).
title
stringAdditional description of the log entry message.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Write(object, string)
Write a new log entry to a specific category.
public void Write(object message, string category)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
Write(object, string, IDictionary<string, object>)
Write a new log entry to a specific category with a dictionary of extended properties.
public void Write(object message, string category, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Write(object, string, int)
Write a new log entry with a specific category and priority.
public void Write(object message, string category, int priority)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
Write(object, string, int, IDictionary<string, object>)
Write a new log entry to with a specific category, priority and a dictionary of extended properties.
public void Write(object message, string category, int priority, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Write(object, string, int, int)
Write a new log entry with a specific category, priority and event id.
public void Write(object message, string category, int priority, int eventId)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
Write(object, string, int, int, TraceEventType)
Write a new log entry with a specific category, priority, event id and severity.
public void Write(object message, string category, int priority, int eventId, TraceEventType severity)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
severity
TraceEventTypeLog entry severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).
Write(object, string, int, int, TraceEventType, string)
Write a new log entry with a specific category, priority, event id, severity and title.
public void Write(object message, string category, int priority, int eventId, TraceEventType severity, string title)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
severity
TraceEventTypeLog message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).
title
stringAdditional description of the log entry message
Write(object, string, int, int, TraceEventType, string, IDictionary<string, object>)
Write a new log entry with a specific category, priority, event Id, severity title and dictionary of extended properties.
public void Write(object message, string category, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)
Parameters
message
objectMessage body to log. Value from ToString() method from message object.
category
stringCategory name used to route the log entry to a one or more trace listeners.
priority
intOnly messages must be above the minimum priority are processed.
eventId
intEvent number or identifier.
severity
TraceEventTypeLog message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).
title
stringAdditional description of the log entry message.
properties
IDictionary<string, object>Dictionary of key/value pairs to log.
Examples
The following example demonstrates use of the Write method with a full set of parameters.