LogUtil.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » LogUtil.cs

using System;
using System.Text;
using System.Threading;
using log4net;
using log4net.Config;
using ThoughtWorks.CruiseControl.Core.Util.Log4NetTrace;
using System.Diagnostics;

// This attribute tells log4net to use the settings in the app.config file for configuration
[assembly: XmlConfigurator()]

namespace ThoughtWorks.CruiseControl.Core.Util{
    // TODO: Replace using this class with the ILogger interface and the IoC container.
  public static class Log
  {
    private static ITraceLog logger = TraceLogManager.GetLogger("CruiseControl.NET");
        
        private static bool loggingEnabled = true;

    static Log()
    {

      if (logger.IsDebugEnabled)
      {
        logger.DebugFormat("The trace level is currently set to debug.  "
        + "This will cause CCNet to log at the most verbose level, which is useful for setting up or debugging the server.  "
        + "Once your server is running smoothly, we recommend changing this setting in {0} to a lower level.",
          AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
      }
            
            if (logger.IsTraceEnabled)
            {
                logger.WarnFormat("! ! Tracing is enabled ! !"
                    + "It allows you to sent the developpers of CCNet very detailed information of the program flow. "
                    + "This setting should only be enabled if you want to report a bug with the extra information. "
                    + "When bug reporting is done, it is advised to set the trace setting off. "
                    + "Adjust the setting in {0}", AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            }
        
        }

        /// <summary>
        /// Disables logging
        /// </summary>
        public static void DisableLogging()
        {
            loggingEnabled = false;
        }

        /// <summary>
        /// Enables logging
        /// </summary>
        public static void EnableLogging()
        {
            loggingEnabled = true;
        }

        /// <summary>
        /// Logs at information level
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        public static void Info(string message, params object[] args)
    {
      if (loggingEnabled) logger.Info(string.Format(message,args));
    }

        /// <summary>
        /// Logs at information level
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            if (loggingEnabled) logger.Info(message);
        }

        /// <summary>
        /// Logs at debug level
        /// </summary>
        /// <param name="message"></param>
    public static void Debug(string message)
    {
            if (loggingEnabled) logger.Debug(message);
    }

        /// <summary>
        /// Logs at debug level
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        public static void Debug(string message, params object[] args)
        {
            if (loggingEnabled) logger.Debug(string.Format(message,args));
        }

        /// <summary>
        /// Logs at warning level
        /// </summary>
        /// <param name="message"></param>
    public static void Warning(string message)
    {
            if (loggingEnabled) logger.Warn(message);
    }

        /// <summary>
        /// Logs at warning level
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        public static void Warning(string message, params object[] args)
        {
            if (loggingEnabled) logger.Warn(string.Format(message,args));
        }

        /// <summary>
        /// Logs at warning level
        /// </summary>
        /// <param name="ex"></param>
    public static void Warning(Exception ex)
    {
            if (loggingEnabled) logger.Warn(CreateExceptionMessage(ex));
    }

        /// <summary>
        /// Logs at Error level
        /// </summary>
        /// <param name="message"></param>
    public static void Error(string message)
    {
            logger.Error(message);
    }

        /// <summary>
        /// Logs at error level
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        public static void Error(string message, params object[] args)
        {
            logger.Error(string.Format(message,args));
        }

        /// <summary>
        /// Logs at errorlevel
        /// </summary>
        /// <param name="ex"></param>
    public static void Error(Exception ex)
    {
            logger.Error(CreateExceptionMessage(ex));
    }

        /// <summary>
        /// Logs at trace level
        /// </summary>
        public static void Trace()
        {
            if (loggingEnabled && logger.IsTraceEnabled) logger.TraceFormat(string.Concat(GetCallingClassName(), "Entering"));
        }

        /// <summary>
        /// Logs at trace level
        /// </summary>
        /// <param name="message"></param>
        public static void Trace(string message)
        {
            if (loggingEnabled && logger.IsTraceEnabled)  logger.TraceFormat(string.Concat(GetCallingClassName() ,message));
        }

        /// <summary>
        /// Logs at trace level
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        public static void Trace(string message, params object[] args)
        {
            if (loggingEnabled && logger.IsTraceEnabled) logger.TraceFormat(string.Concat(GetCallingClassName(), message), args);
        }

        public static TraceBlock StartTrace()
        {
            return new TraceBlock(logger, GetCallingClassName());
        }

        public static TraceBlock StartTrace(string message, params object[] args)
        {
            return new TraceBlock(logger, GetCallingClassName(), string.Format(message, args));
        }

        private static string GetCallingClassName()
        {
            StackTrace Stack = default(StackTrace);
            StackFrame CurrentFrame = default(StackFrame);
            string myAssemblyName = null;
            string myClassName = null;
            string myMethodName = null;

            try
            {
                Stack = new StackTrace();
                CurrentFrame = Stack.GetFrame(2);
                myAssemblyName = CurrentFrame.GetMethod().ReflectedType.Assembly.FullName.Split(',')[0];
                myClassName = CurrentFrame.GetMethod().ReflectedType.ToString();
                myMethodName = CurrentFrame.GetMethod().Name;
            }
            catch
            {
                myClassName = "";
                myMethodName = "";
            }

            return string.Format("{0} - {1}.{2} : ", myAssemblyName, myClassName, myMethodName);
        }

    private static string CreateExceptionMessage(Exception ex)
    {
      if (ex is ThreadAbortException)
      {
        return "Thread aborted for Project: " + Thread.CurrentThread.Name;
      }

      StringBuilder buffer = new StringBuilder();
      buffer.Append(GetExceptionAlertMessage(ex));
      buffer.Append(ex.Message).Append(Environment.NewLine);
      buffer.Append("----------").Append(Environment.NewLine);
      buffer.Append(ex.ToString()).Append(Environment.NewLine);
      buffer.Append("----------").Append(Environment.NewLine);
      return buffer.ToString();
    }
    
    private static string GetExceptionAlertMessage(Exception ex)
    {
      return (ex is CruiseControlException) ? "Exception: " : "INTERNAL ERROR: ";
    }

        /// <summary>
        /// A class for putting a trace call in a block.
        /// </summary>
        public class TraceBlock
            : IDisposable
        {
            private string methodName;
            private ITraceLog logger;

            /// <summary>
            /// Initializes a new instance of the <see cref="TraceBlock"/> class.
            /// </summary>
            /// <param name="logger">The underlying logger to use.</param>
            /// <param name="methodName">The name of the method that is being traced;</param>
            public TraceBlock(ITraceLog logger, string methodName)
            {
                this.logger = logger;
                this.methodName = methodName;
                if (this.logger.IsTraceEnabled)
                {
                    this.logger.TraceFormat("Entering {0}", methodName);
                }
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="TraceBlock"/> class.
            /// </summary>
            /// <param name="logger">The underlying logger to use.</param>
            /// <param name="methodName">The name of the method that is being traced;</param>
            /// <param name="message"></param>
            public TraceBlock(ITraceLog logger, string methodName, string message)
            {
                this.logger = logger;
                this.methodName = methodName;
                if (this.logger.IsTraceEnabled)
                {
                    this.logger.TraceFormat("Entering {0}: {1}", methodName, message);
                }
            }

            /// <summary>
            /// Disposes of the trace block.
            /// </summary>
            public void Dispose()
            {
                if (this.logger.IsTraceEnabled)
                {
                    this.logger.TraceFormat("Exiting {0}", methodName);
                }
            }
        }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.