-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
310301c
commit af4b243
Showing
8 changed files
with
416 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#region License | ||
|
||
/* | ||
* Copyright � 2002-2009 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#endregion | ||
|
||
using System; | ||
using Common.Logging.Configuration; | ||
|
||
namespace Common.Logging.XSimple | ||
{ | ||
/// <summary> | ||
/// Factory for creating <see cref="ILog" /> instances that write data to <see cref="Console.Out" />. | ||
/// </summary> | ||
/// <remarks> | ||
/// <example> | ||
/// Below is an example how to configure this adapter: | ||
/// <code> | ||
/// <configuration> | ||
/// | ||
/// <configSections> | ||
/// <sectionGroup name="common"> | ||
/// <section name="logging" | ||
/// type="Common.Logging.ConfigurationSectionHandler, Common.Logging" | ||
/// requirePermission="false" /> | ||
/// </sectionGroup> | ||
/// </configSections> | ||
/// | ||
/// <common> | ||
/// <logging> | ||
/// <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> | ||
/// <arg key="level" value="ALL" /> | ||
/// </factoryAdapter> | ||
/// </logging> | ||
/// </common> | ||
/// | ||
/// </configuration> | ||
/// </code> | ||
/// </example> | ||
/// </remarks> | ||
/// <seealso cref="AbstractSimpleLoggerFactoryAdapter"/> | ||
/// <seealso cref="LogManager.Adapter"/> | ||
/// <seealso cref="ConfigurationSectionHandler"/> | ||
/// <author>Gilles Bayon</author> | ||
/// <author>Mark Pollack</author> | ||
/// <author>Erich Eichinger</author> | ||
public class ConsoleOutLoggerFactoryAdapter : Simple.AbstractSimpleLoggerFactoryAdapter | ||
{ | ||
#if !SILVERLIGHT | ||
private readonly bool useColor; | ||
|
||
#endif | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConsoleOutLoggerFactoryAdapter"/> class using default | ||
/// settings. | ||
/// </summary> | ||
public ConsoleOutLoggerFactoryAdapter() | ||
: base(null) | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConsoleOutLoggerFactoryAdapter"/> class. | ||
/// </summary> | ||
/// <remarks> | ||
/// Looks for level, showDateTime, showLogName, dateTimeFormat items from | ||
/// <paramref name="properties" /> for use when the GetLogger methods are called. | ||
/// <see cref="ConfigurationSectionHandler"/> for more information on how to use the | ||
/// standard .NET application configuraiton file (App.config/Web.config) | ||
/// to configure this adapter. | ||
/// </remarks> | ||
/// <param name="properties">The name value collection, typically specified by the user in | ||
/// a configuration section named common/logging.</param> | ||
public ConsoleOutLoggerFactoryAdapter(NameValueCollection properties) | ||
: base(properties) | ||
{ } | ||
|
||
#if false | ||
/// <summary> | ||
/// Constructor for binary backwards compatibility with non-portableversions | ||
/// </summary> | ||
/// <param name="properties">The properties.</param> | ||
[Obsolete("Use Constructor taking Common.Logging.Configuration.NameValueCollection instead")] | ||
public ConsoleOutLoggerFactoryAdapter(System.Collections.Specialized.NameValueCollection properties) | ||
: this(NameValueCollectionHelper.ToCommonLoggingCollection(properties)) | ||
{ } | ||
#endif | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AbstractSimpleLoggerFactoryAdapter"/> class with | ||
/// default settings for the loggers created by this factory. | ||
/// </summary> | ||
public ConsoleOutLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool showLogName, bool showLevel, string dateTimeFormat) | ||
: base(level, showDateTime, showLogName, showLevel, dateTimeFormat) | ||
{ } | ||
|
||
#if !SILVERLIGHT | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AbstractSimpleLoggerFactoryAdapter"/> class with | ||
/// default settings for the loggers created by this factory. | ||
/// </summary> | ||
public ConsoleOutLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool showLogName, bool showLevel, string dateTimeFormat, bool useColor) | ||
: this(level, showDateTime, showLogName, showLevel, dateTimeFormat) | ||
{ | ||
this.useColor = useColor; | ||
} | ||
|
||
#endif | ||
/// <summary> | ||
/// Creates a new <see cref="ConsoleOutLogger"/> instance. | ||
/// </summary> | ||
protected override ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat) | ||
{ | ||
#if !SILVERLIGHT | ||
ILog log = new ConsoleOutLogger(name, level, showLevel, showDateTime, showLogName, dateTimeFormat, this.useColor); | ||
#else | ||
ILog log = new ConsoleOutLogger(name, level, showLevel, showDateTime, showLogName, dateTimeFormat); | ||
#endif | ||
return log; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#region License | ||
|
||
/* | ||
* Copyright � 2002-2009 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#endregion | ||
|
||
using System; | ||
#if !SILVERLIGHT | ||
using System.Collections.Generic; | ||
#endif | ||
using System.Text; | ||
|
||
namespace Common.Logging.XSimple | ||
{ | ||
/// <summary> | ||
/// Sends log messages to <see cref="Console.Out" />. | ||
/// </summary> | ||
/// <author>Gilles Bayon</author> | ||
#if PORTABLE | ||
#else | ||
[Serializable] | ||
#endif | ||
public class ConsoleOutLogger : Simple.AbstractSimpleLogger | ||
{ | ||
#if !SILVERLIGHT | ||
private static readonly Dictionary<LogLevel, ConsoleColor> colors = new Dictionary<LogLevel, ConsoleColor> | ||
{ | ||
{ LogLevel.Fatal, ConsoleColor.Red }, | ||
{ LogLevel.Error, ConsoleColor.Yellow }, | ||
{ LogLevel.Warn, ConsoleColor.Magenta }, | ||
{ LogLevel.Info, ConsoleColor.White }, | ||
{ LogLevel.Debug, ConsoleColor.Gray }, | ||
{ LogLevel.Trace, ConsoleColor.DarkGray }, | ||
}; | ||
|
||
private readonly bool useColor; | ||
|
||
#endif | ||
/// <summary> | ||
/// Creates and initializes a logger that writes messages to <see cref="Console.Out" />. | ||
/// </summary> | ||
/// <param name="logName">The name, usually type name of the calling class, of the logger.</param> | ||
/// <param name="logLevel">The current logging threshold. Messages recieved that are beneath this threshold will not be logged.</param> | ||
/// <param name="showLevel">Include the current log level in the log message.</param> | ||
/// <param name="showDateTime">Include the current time in the log message.</param> | ||
/// <param name="showLogName">Include the instance name in the log message.</param> | ||
/// <param name="dateTimeFormat">The date and time format to use in the log message.</param> | ||
public ConsoleOutLogger(string logName, LogLevel logLevel, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat) | ||
: base(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat) | ||
{ | ||
} | ||
|
||
#if !SILVERLIGHT | ||
/// <summary> | ||
/// Creates and initializes a logger that writes messages to <see cref="Console.Out" />. | ||
/// </summary> | ||
/// <param name="logName">The name, usually type name of the calling class, of the logger.</param> | ||
/// <param name="logLevel">The current logging threshold. Messages recieved that are beneath this threshold will not be logged.</param> | ||
/// <param name="showLevel">Include the current log level in the log message.</param> | ||
/// <param name="showDateTime">Include the current time in the log message.</param> | ||
/// <param name="showLogName">Include the instance name in the log message.</param> | ||
/// <param name="dateTimeFormat">The date and time format to use in the log message.</param> | ||
/// <param name="useColor">Use color when writing the log message.</param> | ||
public ConsoleOutLogger(string logName, LogLevel logLevel, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat, bool useColor) | ||
: this(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat) | ||
{ | ||
this.useColor = useColor; | ||
} | ||
|
||
#endif | ||
/// <summary> | ||
/// Do the actual logging by constructing the log message using a <see cref="StringBuilder" /> then | ||
/// sending the output to <see cref="Console.Out" />. | ||
/// </summary> | ||
/// <param name="level">The <see cref="LogLevel" /> of the message.</param> | ||
/// <param name="message">The log message.</param> | ||
/// <param name="e">An optional <see cref="Exception" /> associated with the message.</param> | ||
protected override void WriteInternal(LogLevel level, object message, Exception e) | ||
{ | ||
// Use a StringBuilder for better performance | ||
StringBuilder sb = new StringBuilder(); | ||
FormatOutput(sb, level, message, e); | ||
|
||
// Print to the appropriate destination | ||
#if !SILVERLIGHT | ||
ConsoleColor color; | ||
if (this.useColor && colors.TryGetValue(level, out color)) | ||
{ | ||
var originalColor = Console.ForegroundColor; | ||
try | ||
{ | ||
Console.ForegroundColor = color; | ||
Console.Out.WriteLine(sb.ToString()); | ||
return; | ||
} | ||
finally | ||
{ | ||
Console.ForegroundColor = originalColor; | ||
} | ||
} | ||
|
||
#endif | ||
Console.Out.WriteLine(sb.ToString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.