Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Logging
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv9622/Logging
Added Files:
AbstractLoggingAdvice.cs SimpleLoggingAdvice.cs
Log Message:
Update to Common.Logging 1.2
Add Logging advice
Refactoring of ExceptionHandlingAdvice
start of retry advice
misc improvements to spring.aop
--- NEW FILE: SimpleLoggingAdvice.cs ---
#region License
/*
* Copyright 2002-2007 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 System.Reflection;
using System.Text;
using AopAlliance.Intercept;
using Common.Logging;
namespace Spring.Aspects.Logging
{
/// <summary>
/// This is
/// </summary>
/// <remarks>
///
/// </remarks>
/// <author>Mark Pollack</author>
/// <version>$Id: SimpleLoggingAdvice.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public class SimpleLoggingAdvice : AbstractLoggingAdvice
{
#region Fields
/// <summary>
/// Flag to indicate if unique identifier should be in the log message.
/// </summary>
private bool logUniqueIdentifier;
/// <summary>
/// Flag to indicate if the execution time should be in the log message.
/// </summary>
private bool logExecutionTime;
/// <summary>
/// Flag to indicate if the method arguments should be in the log message.
/// </summary>
private bool logMethodArguments;
/// <summary>
/// The seperator string to use for delmiting log message fields.
/// </summary>
private string seperator = ", ";
/// <summary>
/// The log level to use for logging the entry, exit, exception messages.
/// </summary>
private LogLevel logLevel = LogLevel.Trace;
#endregion
#region Constructor(s)
/// <summary>
/// Initializes a new instance of the <see cref="SimpleLoggingAdvice"/> class.
/// </summary>
public SimpleLoggingAdvice()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimpleLoggingAdvice"/> class.
/// </summary>
/// <param name="useDynamicLogger">if set to <c>true</c> to use dynamic logger, if
/// <c>false</c> use static logger.</param>
public SimpleLoggingAdvice(bool useDynamicLogger)
{
UseDynamicLogger = useDynamicLogger;
}
#endregion
/// <summary>
/// Gets or sets a value indicating whether to log a unique identifier with the log message.
/// </summary>
/// <value><c>true</c> if [log unique identifier]; otherwise, <c>false</c>.</value>
public bool LogUniqueIdentifier
{
get { return logUniqueIdentifier; }
set { logUniqueIdentifier = value; }
}
/// <summary>
/// Gets or sets a value indicating whether to log execution time.
/// </summary>
/// <value><c>true</c> if log execution time; otherwise, <c>false</c>.</value>
public bool LogExecutionTime
{
get { return logExecutionTime; }
set { logExecutionTime = value; }
}
/// <summary>
/// Gets or sets a value indicating whether log method arguments.
/// </summary>
/// <value><c>true</c> if log method arguments]; otherwise, <c>false</c>.</value>
public bool LogMethodArguments
{
get { return logMethodArguments; }
set { logMethodArguments = value; }
}
/// <summary>
/// Gets or sets the seperator string to use for delmiting log message fields.
/// </summary>
/// <value>The seperator.</value>
public string Seperator
{
get { return seperator; }
set { seperator = value; }
}
/// <summary>
/// Gets or sets the entry log level.
/// </summary>
/// <value>The entry log level.</value>
public LogLevel LogLevel
{
get { return logLevel; }
set { logLevel = value; }
}
#region Protected Methods
/// <summary>
/// Subclasses must override this method to perform any tracing around the supplied
/// IMethodInvocation.
/// </summary>
/// <param name="invocation">The method invocation to log</param>
/// <param name="log">The log to write messages to</param>
/// <returns>
/// The result of the call to IMethodInvocation.Proceed()
/// </returns>
/// <remarks>
/// Subclasses are resonsible for ensuring that the IMethodInvocation actually executes
/// by calling IMethodInvocation.Proceed().
/// <para>
/// By default, the passed-in ILog instance will have log level
/// "trace" enabled. Subclasses do not have to check for this again, unless
/// they overwrite the IsInterceptorEnabled method to modify
/// the default behavior.
/// </para>
/// </remarks>
/// <exception cref="System.Exception">
/// If any of the interceptors in the chain or the target object itself
/// throws an exception.
/// </exception>
protected override object InvokeUnderLog(IMethodInvocation invocation, ILog log)
{
object returnValue = null;
bool exitThroughException = false;
DateTime startTime = DateTime.Now;
string uniqueIdentifier = null;
if (LogUniqueIdentifier)
{
uniqueIdentifier = CreateUniqueIdentifier();
}
try
{
WriteToLog(LogLevel, log, GetEntryMessage(invocation, uniqueIdentifier), null);
returnValue = invocation.Proceed();
return returnValue;
} catch (Exception e)
{
TimeSpan executionTimeSpan = DateTime.Now - startTime;
WriteToLog(LogLevel, log, GetExceptionMessage(invocation, e, executionTimeSpan, uniqueIdentifier), e);
exitThroughException = true;
throw;
}
finally
{
if (!exitThroughException)
{
TimeSpan executionTimeSpan = DateTime.Now - startTime;
WriteToLog(LogLevel, log, GetExitMessage(invocation, returnValue, executionTimeSpan, uniqueIdentifier), null);
}
}
}
/// <summary>
/// Creates a unique identifier.
/// </summary>
/// <remarks>
/// Default implementation uses Guid.NewGuid(). Subclasses may override to provide an alternative
/// ID generation implementation.
/// </remarks>
/// <returns>A unique identifier</returns>
protected virtual string CreateUniqueIdentifier()
{
return Guid.NewGuid().ToString();
}
/// <summary>
/// Gets the entry message to log
/// </summary>
/// <param name="invocation">The invocation.</param>
/// <param name="idString">The id string.</param>
/// <returns>The entry log message</returns>
protected virtual string GetEntryMessage(IMethodInvocation invocation, string idString)
{
StringBuilder sb = new StringBuilder(128);
sb.Append("Entering ");
AppendCommonInformation(sb, invocation, idString);
if (logMethodArguments)
{
sb.Append(GetMethodArgumentAsString(invocation)).Append(Seperator);
}
return RemoveLastSeparator(sb, Seperator);
}
/// <summary>
/// Gets the exception message.
/// </summary>
/// <param name="invocation">The method invocation.</param>
/// <param name="e">The thown exception.</param>
/// <param name="executionTimeSpan">The execution time span.</param>
/// <param name="idString">The id string.</param>
/// <returns>The exception log message.</returns>
protected virtual string GetExceptionMessage(IMethodInvocation invocation, Exception e, TimeSpan executionTimeSpan, string idString)
{
StringBuilder sb = new StringBuilder(128);
sb.Append("Exception thrown in ");
sb.Append(invocation.Method.Name);
AppendCommonInformation(sb, invocation, idString);
if (LogExecutionTime)
{
sb.Append(executionTimeSpan.TotalMilliseconds);
}
return sb.ToString();
}
/// <summary>
/// Gets the exit log message.
/// </summary>
/// <param name="invocation">The method invocation.</param>
/// <param name="returnValue">The return value.</param>
/// <param name="executionTimeSpan">The execution time span.</param>
/// <param name="idString">The id string.</param>
/// <returns>the exit log message</returns>
protected virtual string GetExitMessage(IMethodInvocation invocation, object returnValue, TimeSpan executionTimeSpan, string idString)
{
StringBuilder sb = new StringBuilder(128);
sb.Append("Exiting ");
AppendCommonInformation(sb, invocation, idString);
if (LogExecutionTime)
{
sb.Append(executionTimeSpan.TotalMilliseconds).Append(" ms").Append(Seperator);
}
sb.Append("return=").Append(returnValue);
return sb.ToString();
}
/// <summary>
/// Appends common information across entry,exit, exception logging
/// </summary>
/// <remarks>Add method name and unique identifier if required.</remarks>
/// <param name="sb">The string buffer building logging message.</param>
/// <param name="invocation">The method invocation.</param>
/// <param name="idString">The unique identifier string.</param>
protected virtual void AppendCommonInformation(StringBuilder sb, IMethodInvocation invocation, string idString)
{
sb.Append(invocation.Method.Name);
if (LogUniqueIdentifier)
{
sb.Append(Seperator).Append(idString);
}
sb.Append(Seperator);
}
/// <summary>
/// Gets the method argument as argumen name/value pairs.
/// </summary>
/// <param name="invocation">The method invocation.</param>
/// <returns>string for logging method argument name and values.</returns>
protected virtual string GetMethodArgumentAsString(IMethodInvocation invocation)
{
StringBuilder sb = new StringBuilder(128);
ParameterInfo[] parameterInfos = invocation.Method.GetParameters();
object[] argValues = invocation.Arguments;
for (int i=0; i< parameterInfos.Length; i++)
{
sb.Append(parameterInfos[i].Name).Append("=").Append(argValues[i]);
if (i != parameterInfos.Length) sb.Append("; ");
}
return RemoveLastSeparator(sb, "; ");
}
#endregion
#region Private Methods
private string RemoveLastSeparator(StringBuilder sb, string seperator)
{
string s = sb.ToString();
if (s.EndsWith(seperator))
{
return s.Remove(s.Length - seperator.Length);
}
else
{
return s;
}
}
private void WriteToLog(LogLevel logLevel, ILog log, string text, Exception e)
{
switch (logLevel)
{
case LogLevel.All:
case LogLevel.Trace:
if (log.IsTraceEnabled)
{
if (e == null) log.Trace(text); else log.Trace(text, e);
}
break;
case LogLevel.Debug:
if (log.IsDebugEnabled)
{
if (e == null) log.Debug(text); else log.Debug(text, e);
}
break;
case LogLevel.Error:
if (log.IsErrorEnabled)
{
if (e == null) log.Error(text); else log.Error(text, e);
}
break;
case LogLevel.Fatal:
if (log.IsFatalEnabled)
{
if (e == null) log.Fatal(text); else log.Fatal(text, e);
}
break;
case LogLevel.Info:
if (log.IsInfoEnabled)
{
if (e == null) log.Info(text); else log.Info(text, e);
}
break;
case LogLevel.Warn:
if (log.IsWarnEnabled)
{
if (e == null) log.Warn(text); else log.Warn(text, e);
}
break;
case LogLevel.Off:
default:
break;
}
}
#endregion
}
}
--- NEW FILE: AbstractLoggingAdvice.cs ---
#region License
/*
* Copyright 2002-2007 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 System.Reflection;
using AopAlliance.Intercept;
using Common.Logging;
using Spring.Aop.Framework;
namespace Spring.Aspects.Logging
{
/// <summary>
/// Abstract base class for logging advice
/// </summary>
/// <remarks>
///
/// </remarks>
/// <author>Mark Pollack</author>
/// <version>$Id: AbstractLoggingAdvice.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public abstract class AbstractLoggingAdvice : IMethodInterceptor
{
#region Fields
/// <summary>
/// The default <code>ILog</code> instance used to write logging messages.
/// </summary>
protected ILog defaultLogger = LogManager.GetLogger(MethodInfo.GetCurrentMethod().DeclaringType);
/// <summary>
/// Indicates whether or not proxy type names should be hidden when using dynamic loggers.
/// </summary>
private bool hideProxyTypeNames = false;
#endregion
#region Properties
/// <summary>
/// Sets a value indicating whether to use a dynamic logger or static logger
/// </summary>
/// <remarks>Default is to use a static logger.
/// <para>
/// Used to determine which ILog instance should be used to write log messages for
/// a particular method invocation: a dynamic one for the Type getting called,
/// or a static one for the Type of the trace interceptor.
/// </para>
/// <para>
/// Specify either this property or LoggerName, not both.
/// </para>
/// </remarks>
/// <value><c>true</c> if use dynamic logger; otherwise, <c>false</c>.</value>
public bool UseDynamicLogger
{
set
{
defaultLogger = (value ? null : LogManager.GetLogger(GetType()));
}
}
/// <summary>
/// Sets the name of the logger to use.
/// </summary>
/// <remarks>
/// The name will be passed to the underlying logging implementation through Common.Logging,
/// getting interpreted as the log category according to the loggers configuration.
/// <para>
/// This can be specified to not log into the category of a Type (whether this
/// interceptor's class or the class getting called) but rather to a specific named category.
/// </para>
/// <para>
/// Specify either this property or UseDynamicLogger, but not both.
/// </para>
/// </remarks>
/// <value>The name of the logger.</value>
public string LoggerName
{
set
{
defaultLogger = LogManager.GetLogger(value);
}
}
/// <summary>
/// Sets a value indicating whether hide proxy type names (whenever possible)
/// when using dynamic loggers, i.e. property UseDynamicLogger is set to true.
/// </summary>
/// <value><c>true</c> if [hide proxy type names]; otherwise, <c>false</c>.</value>
public bool HideProxyTypeNames
{
set { hideProxyTypeNames = value; }
}
#endregion
#region Methods
/// <summary>
/// Adds logging to the method invocation.
/// </summary>
/// <remarks>
/// The method IsInterceptorEnabled is called
/// as an optimization to determine if logging should be applied. If logging should be
/// applied, the method invocation is passed to the InvokeUnderLog method for handling.
/// If not, the method proceeds as normal.
/// </remarks>
/// <param name="invocation">
/// The method invocation that is being intercepted.
/// </param>
/// <returns>
/// The result of the call to the
/// <see cref="AopAlliance.Intercept.IJoinpoint.Proceed"/> method of
/// the supplied <paramref name="invocation"/>; this return value may
/// well have been intercepted by the interceptor.
/// </returns>
/// <exception cref="System.Exception">
/// If any of the interceptors in the chain or the target object itself
/// throws an exception.
/// </exception>
public object Invoke(IMethodInvocation invocation)
{
object o = invocation.This;
ILog log = GetLoggerForInvocation(invocation);
if (IsInterceptorEnabled(invocation, log))
{
return InvokeUnderLog(invocation, log);
}
else
{
return invocation.Proceed();
}
}
/// <summary>
/// Determines whether the interceptor is enabled for the specified invocation, that
/// is, whether the method InvokeUnderLog is called.
/// </summary>
/// <remarks>The default behavior is to check whether the given ILog instance
/// is enabled by calling IsLogEnabled, whose default behavior is to check if
/// the TRACE level of logging is enabled. Subclasses</remarks>
/// <param name="invocation">The invocation.</param>
/// <param name="log">The log to write messages to</param>
/// <returns>
/// <c>true</c> if [is interceptor enabled] [the specified invocation]; otherwise, <c>false</c>.
/// </returns>
protected virtual bool IsInterceptorEnabled(IMethodInvocation invocation, ILog log)
{
return IsLogEnabled(log);
}
/// <summary>
/// Determines whether the given log is enabled.
/// </summary>
/// <remarks>
/// Default is true when the trace level is enabled. Subclasses may override this
/// to change the level at which logging occurs, or return true to ignore level
/// checks.</remarks>
/// <param name="log">The log instance to check.</param>
/// <returns>
/// <c>true</c> if log is for a given log level; otherwise, <c>false</c>.
/// </returns>
private bool IsLogEnabled(ILog log)
{
return log.IsTraceEnabled;
}
/// <summary>
/// Subclasses must override this method to perform any tracing around the supplied
/// IMethodInvocation.
/// </summary>
/// <remarks>
/// Subclasses are resonsible for ensuring that the IMethodInvocation actually executes
/// by calling IMethodInvocation.Proceed().
/// <para>
/// By default, the passed-in ILog instance will have log level
/// "trace" enabled. Subclasses do not have to check for this again, unless
/// they overwrite the IsInterceptorEnabled method to modify
/// the default behavior.
/// </para>
/// </remarks>
/// <param name="invocation">The method invocation to log</param>
/// <param name="log">The log to write messages to</param>
/// <returns>The result of the call to IMethodInvocation.Proceed()
/// </returns>
/// <exception cref="System.Exception">
/// If any of the interceptors in the chain or the target object itself
/// throws an exception.
/// </exception>
protected abstract object InvokeUnderLog(IMethodInvocation invocation, ILog log);
/// <summary>
/// Gets the appropriate log instance to use for the given IMethodInvocation.
/// </summary>
/// <remarks>
/// If the UseDynamicLogger property is set to true, the ILog instance will be
/// for the target class of the IMethodInvocation, otherwise the log will be the
/// default static logger.
/// </remarks>
/// <param name="invocation">The method invocation being logged.</param>
/// <returns>The ILog instance to use.</returns>
protected virtual ILog GetLoggerForInvocation(IMethodInvocation invocation)
{
if (defaultLogger != null)
{
return defaultLogger;
}
else
{
object target = invocation.This;
Type logCategoryType = target.GetType();
if (hideProxyTypeNames)
{
logCategoryType = AopUtils.GetTargetType(target);
}
return LogManager.GetLogger(logCategoryType);
}
}
#endregion
}
}
|