Table of Contents

Class Logger

Namespace
Microsoft.Practices.EnterpriseLibrary.Logging
Assembly
Microsoft.Practices.EnterpriseLibrary.Logging.dll

Facade for writing a log entry to one or more TraceListeners. This class is sealed.

public static class Logger
Inheritance
Logger
Inherited Members

Properties

Writer

Gets the instance of LogWriter used by the facade.

public static LogWriter Writer { get; }

Property Value

LogWriter

Methods

FlushContextItems()

Empty the context items dictionary.

[SecurityCritical]
public static void FlushContextItems()

GetFilter(string)

Returns the filter named name.

public static ILogFilter GetFilter(string name)

Parameters

name string

The 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 static 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 static T GetFilter<T>(string name) where T : class, ILogFilter

Parameters

name string

The name of the filter required.

Returns

T

The instance of T named name in the filters collection, or null if there is no such instance.

Type Parameters

T

The type of filter required.

IsLoggingEnabled()

Query whether logging is enabled.

public static bool IsLoggingEnabled()

Returns

bool
  true
if logging is enabled.

Reset()

Reset the writer used by the Logger facade.

public static void Reset()

Remarks

Resetting the writer disposes the current writer and sets the reference to null so further attempts to use the facade will fail until it is re-initialized.

This method should be invoked only when no other operations are being performed through the facade.

Threads that already acquired the reference to the old writer will fail when it gets disposed.

SetContextItem(object, object)

Add a key/value pair to the System.Runtime.Remoting.Messaging.CallContext dictionary.
Context items will be recorded with every log entry.

[SecurityCritical]
public static void SetContextItem(object key, object value)

Parameters

key object

Hashtable key

value object

Value. Objects will be serialized.

Examples

The following example demonstrates use of the AddContextItem method.

Logger.SetContextItem("SessionID", myComponent.SessionId);

SetLogWriter(LogWriter, bool)

Sets the log writer.

public static void SetLogWriter(LogWriter logWriter, bool throwIfSet = true)

Parameters

logWriter LogWriter

The log writer.

throwIfSet bool

true to throw an exception if the writer is already set; otherwise false. Defaults to true.

Exceptions

InvalidOperationException

The factory is already set, and throwIfSet is true.

ShouldLog(LogEntry)

Query whether a LogEntry shold be logged.

public static bool ShouldLog(LogEntry log)

Parameters

log LogEntry

The log entry to check

Returns

bool

Returns

true
if the entry should be logged.

Write(LogEntry)

Write a new log entry as defined in the LogEntry parameter.

public static void Write(LogEntry log)

Parameters

log LogEntry

Log entry object to write.

Examples

The following examples demonstrates use of the Write method using a LogEntry type.

LogEntry log = new LogEntry();
log.Category = "MyCategory1";
log.Message = "My message body";
log.Severity = TraceEventType.Error;
log.Priority = 100;
Logger.Write(log);

Write(object)

Write a new log entry to the default category.

public static void Write(object message)

Parameters

message object

Message body to log. Value from ToString() method from message object.

Examples

The following example demonstrates use of the Write method with one required parameter, message.

Logger.Write("My message body");

Write(object, ICollection<string>)

Write a new log entry to a specific collection of categories.

public static void Write(object message, ICollection<string> categories)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

Write(object, ICollection<string>, IDictionary<string, object>)

Write a new log entry to a specific collection of categories with a dictionary of extended properties.

public static void Write(object message, ICollection<string> categories, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<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, ICollection<string>, int)

Write a new log entry with a specific collection of categories and priority.

public static void Write(object message, ICollection<string> categories, int priority)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

Write(object, ICollection<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 static void Write(object message, ICollection<string> categories, int priority, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

properties IDictionary<string, object>

Dictionary of key/value pairs to log.

Write(object, ICollection<string>, int, int)

Write a new log entry with a specific collection of categories, priority and event id.

public static void Write(object message, ICollection<string> categories, int priority, int eventId)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

Write(object, ICollection<string>, int, int, TraceEventType)

Write a new log entry with a specific collection of categories, priority, event id and severity.

public static void Write(object message, ICollection<string> categories, int priority, int eventId, TraceEventType severity)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

severity TraceEventType

Log entry severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).

Write(object, ICollection<string>, int, int, TraceEventType, string)

Write a new log entry with a specific collection of categories, priority, event id, severity and title.

public static void Write(object message, ICollection<string> categories, int priority, int eventId, TraceEventType severity, string title)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

severity TraceEventType

Log message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).

title string

Additional description of the log entry message

Write(object, ICollection<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 static void Write(object message, ICollection<string> categories, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

categories ICollection<string>

Category names used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

severity TraceEventType

Log message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).

title string

Additional 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.

Write(object, IDictionary<string, object>)

Write a new log entry and a dictionary of extended properties.

public static void Write(object message, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

properties IDictionary<string, object>

Dictionary of key/value pairs to log.

Write(object, string)

Write a new log entry to a specific category.

public static void Write(object message, string category)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category 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 static void Write(object message, string category, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category 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 static void Write(object message, string category, int priority)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category name used to route the log entry to a one or more trace listeners.

priority int

Only 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 static void Write(object message, string category, int priority, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category name used to route the log entry to a one or more trace listeners.

priority int

Only 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 static void Write(object message, string category, int priority, int eventId)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category name used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

Write(object, string, int, int, TraceEventType)

Write a new log entry with a specific category, priority, event id and severity.

public static void Write(object message, string category, int priority, int eventId, TraceEventType severity)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category name used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

severity TraceEventType

Log 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 static void Write(object message, string category, int priority, int eventId, TraceEventType severity, string title)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category name used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

severity TraceEventType

Log message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).

title string

Additional 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 static void Write(object message, string category, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)

Parameters

message object

Message body to log. Value from ToString() method from message object.

category string

Category name used to route the log entry to a one or more trace listeners.

priority int

Only messages must be above the minimum priority are processed.

eventId int

Event number or identifier.

severity TraceEventType

Log message severity as a TraceEventType enumeration. (Unspecified, Information, Warning or Error).

title string

Additional 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.