Enhanced C#
Language of your choice: library documentation
Nested classes | Public static fields | Properties | Static Public Member Functions | List of all members
Loyc.MessageSink Class Reference

Keeps track of the default message sink (Default); contains a series of helper methods; and contains extension methods modeled after log4net: Fatal, Error, Warn, Info, Debug. More...


Source file:

Remarks

Keeps track of the default message sink (Default); contains a series of helper methods; and contains extension methods modeled after log4net: Fatal, Error, Warn, Info, Debug.

Helpful article: http://core.loyc.net/essentials/messagesink.html

See also
IMessageSink

Nested classes

struct  PushedCurrent
 Returned by PushCurrent(IMessageSink). More...
 

Public static fields

static readonly TraceMessageSink Trace = TraceMessageSink.Value
 Sends all messages to System.Diagnostics.Trace.WriteLine(string). More...
 
static readonly ConsoleMessageSink Console = ConsoleMessageSink.Value
 Sends all messages to the System.Console.WriteLine(string). More...
 
static readonly NullMessageSink Null = NullMessageSink.Value
 The message sink that discards all messages. More...
 

Properties

static IMessageSink?? Default [get]
 
static IMessageSink?? Current [get, set]
 
static Func< object, string > ContextToString [get]
 Gets the strategy that message sinks should use to convert a context object to a string. More...
 

Static Public Member Functions

static SavedValue< IMessageSinkSetDefault (IMessageSink sink)
 Used to change the MessageSink.Default property temporarily. More...
 
static PushedCurrent PushCurrent (IMessageSink sink)
 
static object LocationOf (object context)
 Returns context.Location if context implements IHasLocation; otherwise, returns context itself. More...
 
static string LocationString (object context)
 
static string GetLocationString (object context)
 Gets the location information from the specified object, or converts the object to a string. This is the default method returned from ContextToString. More...
 
static SavedValue< Func< object, string > > SetContextToString (Func< object, string > contextToString)
 Sets the strategy that message sinks should use to convert a context object to a string. More...
 
static bool IsFatalEnabled< C > (this IMessageSink< C > sink)
 
static void Fatal< C > (this IMessageSink< C > sink, C context, string format)
 
static void Fatal< C > (this IMessageSink< C > sink, C context, string format, params object[] args)
 
static void Fatal< C > (this IMessageSink< C > sink, C context, string format, object arg0, object arg1=null)
 
static void Fatal (this IMessageSink< object > sink, string format)
 
static void FatalFormat (this IMessageSink< object > sink, string format, params object[] args)
 
static void FatalFormat (this IMessageSink< object > sink, string format, object arg0, object arg1=null)
 
static bool IsErrorEnabled< C > (this IMessageSink< C > sink)
 
static void Error< C > (this IMessageSink< C > sink, C context, string format)
 
static void Error< C > (this IMessageSink< C > sink, C context, string format, params object[] args)
 
static void Error< C > (this IMessageSink< C > sink, C context, string format, object arg0, object arg1=null)
 
static void Error (this IMessageSink< object > sink, string format)
 
static void ErrorFormat (this IMessageSink< object > sink, string format, params object[] args)
 
static void ErrorFormat (this IMessageSink< object > sink, string format, object arg0, object arg1=null)
 
static bool IsWarnEnabled< C > (this IMessageSink< C > sink)
 
static void Warning< C > (this IMessageSink< C > sink, C context, string format)
 
static void Warning< C > (this IMessageSink< C > sink, C context, string format, params object[] args)
 
static void Warning< C > (this IMessageSink< C > sink, C context, string format, object arg0, object arg1=null)
 
static void Warn (this IMessageSink< object > sink, string format)
 
static void WarnFormat (this IMessageSink< object > sink, string format, params object[] args)
 
static void WarnFormat (this IMessageSink< object > sink, string format, object arg0, object arg1=null)
 
static bool IsInfoEnabled< C > (this IMessageSink< C > sink)
 
static void Info< C > (this IMessageSink< C > sink, C context, string format)
 
static void Info< C > (this IMessageSink< C > sink, C context, string format, params object[] args)
 
static void Info< C > (this IMessageSink< C > sink, C context, string format, object arg0, object arg1=null)
 
static void Info (this IMessageSink< object > sink, string format)
 
static void InfoFormat (this IMessageSink< object > sink, string format, params object[] args)
 
static void InfoFormat (this IMessageSink< object > sink, string format, object arg0, object arg1=null)
 
static bool IsDebugEnabled< C > (this IMessageSink< C > sink)
 
static void Debug< C > (this IMessageSink< C > sink, C context, string format)
 
static void Debug< C > (this IMessageSink< C > sink, C context, string format, params object[] args)
 
static void Debug< C > (this IMessageSink< C > sink, C context, string format, object arg0, object arg1=null)
 
static void Debug (this IMessageSink< object > sink, string format)
 
static void DebugFormat (this IMessageSink< object > sink, string format, params object[] args)
 
static void DebugFormat (this IMessageSink< object > sink, string format, object arg0, object arg1=null)
 
static string FormatMessage (Severity type, object context, string format, params object[] args)
 Converts a quadruplet (type, context, format, args) to a single string containing all that information. The format string and the Severity are localized with Localize.Localized(string, object[]). More...
 
static MessageSinkFromDelegate FromDelegate (WriteMessageFn writer, Func< Severity, bool > isEnabled=null)
 Sends all messages to a user-defined method. More...
 
static MessageSinkWithContext WithContext (object context, string messagePrefix=null)
 Creates a message sink that writes to MessageSink.Default with a default context to be used when Write is called with context: null, so that you can use extension methods like Error(string) that do not have any context parameter. More...
 
static MessageSinkWithContext< TContext > WithContext< TContext > (IMessageSink< TContext > target, TContext context, string messagePrefix=null)
 Creates a message sink with a default context to be used when Write is called with context: null, so that you can use extension methods like Error(string) that do not have any context parameter. More...
 
static void Write (this IMessageSink< object > sink, LogMessage msg)
 
static void Write (this IMessageSink< object > sink, ILogMessage msg)
 

Member Function Documentation

◆ FormatMessage()

static string Loyc.MessageSink.FormatMessage ( Severity  type,
object  context,
string  format,
params object[]  args 
)
inlinestatic

Converts a quadruplet (type, context, format, args) to a single string containing all that information. The format string and the Severity are localized with Localize.Localized(string, object[]).

For example, FormatMessage(Severity.Error, "context", "Something happened!") comes out as "Error: context: Something happened!".

References Loyc.MessageSink.ContextToString, and Loyc.Localize.Localized().

◆ FromDelegate()

static MessageSinkFromDelegate Loyc.MessageSink.FromDelegate ( WriteMessageFn  writer,
Func< Severity, bool >  isEnabled = null 
)
inlinestatic

Sends all messages to a user-defined method.

◆ GetLocationString()

static string Loyc.MessageSink.GetLocationString ( object  context)
inlinestatic

Gets the location information from the specified object, or converts the object to a string. This is the default method returned from ContextToString.

Parameters
contextA value whose string representation you want to get.
Returns
If context implements IHasLocation, this converts IHasLocation.Location to a string; if context is null, this method returns null; otherwise it returns context.ToString().

◆ LocationOf()

static object Loyc.MessageSink.LocationOf ( object  context)
inlinestatic

Returns context.Location if context implements IHasLocation; otherwise, returns context itself.

◆ SetContextToString()

static SavedValue<Func<object, string> > Loyc.MessageSink.SetContextToString ( Func< object, string >  contextToString)
inlinestatic

Sets the strategy that message sinks should use to convert a context object to a string.

ContextToString is a thread-local value, but since .NET does not support inheritance of thread-local values, this method also sets the global default used by threads on which this method was never called.

This property follows the Ambient Service Pattern: http://core.loyc.net/essentials/ambient-service-pattern.html

◆ SetDefault()

static SavedValue<IMessageSink> Loyc.MessageSink.SetDefault ( IMessageSink  sink)
inlinestatic

Used to change the MessageSink.Default property temporarily.

using (MessageSink.SetDefault(ConsoleMessageSink.Value))
MessageSink.Default.Write(Severity.Warning, null, "This prints on the console.")

This method sets a thread-local value, but it also sets the global default used by threads on which this method was never called.

This property follows the Ambient Service Pattern: http://core.loyc.net/essentials/ambient-service-pattern.html

◆ WithContext()

static MessageSinkWithContext Loyc.MessageSink.WithContext ( object  context,
string  messagePrefix = null 
)
inlinestatic

Creates a message sink that writes to MessageSink.Default with a default context to be used when Write is called with context: null, so that you can use extension methods like Error(string) that do not have any context parameter.

◆ WithContext< TContext >()

static MessageSinkWithContext<TContext> Loyc.MessageSink.WithContext< TContext > ( IMessageSink< TContext >  target,
TContext  context,
string  messagePrefix = null 
)
inlinestatic

Creates a message sink with a default context to be used when Write is called with context: null, so that you can use extension methods like Error(string) that do not have any context parameter.

Type Constraints
TContext :class 

Member Data Documentation

◆ Console

readonly ConsoleMessageSink Loyc.MessageSink.Console = ConsoleMessageSink.Value
static

Sends all messages to the System.Console.WriteLine(string).

◆ Null

readonly NullMessageSink Loyc.MessageSink.Null = NullMessageSink.Value
static

The message sink that discards all messages.

◆ Trace

readonly TraceMessageSink Loyc.MessageSink.Trace = TraceMessageSink.Value
static

Sends all messages to System.Diagnostics.Trace.WriteLine(string).

Property Documentation

◆ ContextToString

Func<object, string> Loyc.MessageSink.ContextToString
staticget

Gets the strategy that message sinks should use to convert a context object to a string.

Message sinks are commonly used to display error and warning messages, and when you write a message with IMessageSink.Write(), the second parameter is a "context" argument which specifies the object to which the message is related (for example, when writing compiler output, the context might be a node in a syntax tree). Most message sinks display the message in text form (in a log file or terminal), and in that case the best option is to display the location information associated with the context object (e.g. Foo.cpp:45), rather than a string representation of the object itself.

Therefore, message sinks that display a message in text form will call this delegate to convert the context object to a string.

See GetLocationString to learn how the default strategy works, but message sinks should call this delegate rather than GetLocationString().

Referenced by Loyc.MessageSink.FormatMessage().

Loyc.Severity
Severity
A linear scale to categorize the importance and seriousness of messages sent to IMessageSink.
Definition: IMessageSink.cs:123