You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Charles Carroll <ch...@gmail.com> on 2010/01/26 22:05:28 UTC

Trying to invoke this wrapper while doing 200 other things drawing a blank

Lets say I want to invoke this like (look for line labelled // does not work)...

Usually I have no trouble I am just drawing a blank and multitasking badly....


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test.Logger;

namespace ConsoleApplication1
{
    class LoggingTest
    {
        static void Main(string[] args)
        {
            Console.Write("Hello");
            ILogger log = LogManager.GetLogger("testing blah");  //
does not work
            log.Info("{0}","Hello Logger");
            Console.ReadKey();
        }
    }



Here is the wrapper (yeah the Debug.Writes will go away and be
replaced with log4net calls in 30 minutes just want to get calling
syntax for this wrapper now...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using log4net;

namespace Test.Logger
{

    public interface ILogger
    {

        void Debug(string format, params object[] args);
        void Info(string format, params object[] args);
        void Warn(string format, params object[] args);
        void Error(string format, params object[] args);
        void Fatal(string format, params object[] args);

        void Debug(string format, Exception exception, params object[] args);
        void Info(string format, Exception exception, params object[] args);
        void Warn(string format, Exception exception, params object[] args);
        void Error(string format, Exception exception, params object[] args);
        void Fatal(string format, Exception exception, params object[] args);

        bool IsDebugEnabled
        {
            get;
        }
        bool IsInfoEnabled
        {
            get;
        }
        bool IsWarnEnabled
        {
            get;
        }
        bool IsErrorEnabled
        {
            get;
        }
        bool IsFatalEnabled
        {
            get;
        }

    }

    public sealed class LogManager
    {

        public static void Configure(FileInfo configFile)
        {

            if (configFile == null)
            {
                throw new System.ArgumentNullException("configFile");
            }

            System.Diagnostics.EventLog.WriteEntry(string.Format("{0}.LogManager",
Process.GetCurrentProcess().ProcessName), string.Format("Configuring
logging using configuration file {0}", configFile.FullName));

            log4net.Config.XmlConfigurator.ConfigureAndWatch(configFile);


            GetLogger(typeof(LogManager)).Debug("Configured logging
using configuration file {0}", configFile.FullName);
        }

        public static ILogger GetLogger(System.Type type)
        {
            log4net.ILog logger =
log4net.LogManager.GetLogger(System.Reflection.Assembly.GetCallingAssembly(),
type);
            return new Log4NetAdapter(logger);
        }

    }

    internal class Log4NetAdapter : ILogger
    {

        private ILog _inner;

        internal Log4NetAdapter(ILog log4netLogger)
        {
            _inner = log4netLogger;
        }

        public void Debug(string format, params object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format,args));
        }
        public void Info(string format, params object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args));
        }
        public void Warn(string format, params object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args));
        }
        public void Error(string format, params object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args));
        }
        public void Fatal(string format, params object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args));
        }

        public void Debug(string format, Exception exception, params
object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args)
+ exception.Message);
        }

        public void Info(string format, Exception exception, params
object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args)
+ exception.Message);
        }

        public void Warn(string format, Exception exception, params
object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args)
+ exception.Message);
        }

        public void Error(string format, Exception exception, params
object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args)
+ exception.Message);
        }

        public void Fatal(string format, Exception exception, params
object[] args)
        {
            System.Diagnostics.Debug.Write(string.Format(format, args)
+ exception.Message);
        }

        public bool IsDebugEnabled { get; set; }

        public bool IsInfoEnabled { get; set; }

        public bool IsWarnEnabled { get; set; }

        public bool IsErrorEnabled { get; set; }

        public bool IsFatalEnabled { get; set; }

    } // end Log4NetAdapter

} // end namespace

Re: Trying to invoke this wrapper while doing 200 other things drawing a blank

Posted by Charles Carroll <ch...@gmail.com>.
I solved it:

            ILogger blah = LogManager.GetLogger(typeof(System.String));
            blah.Info("Hello");


Then I rewrote it to use an enum instead of discrete methods and just
two methods

WriteMsg(...);
WriteException(..);

    enum MsgType
    {
        Debug,
        Info,
        Warn,
        Error,
        Fatal
    }

On Tue, Jan 26, 2010 at 4:05 PM, Charles Carroll
<ch...@gmail.com> wrote:
> Lets say I want to invoke this like (look for line labelled // does not work)...
>
> Usually I have no trouble I am just drawing a blank and multitasking badly....
>
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using Test.Logger;
>
> namespace ConsoleApplication1
> {
>    class LoggingTest
>    {
>        static void Main(string[] args)
>        {
>            Console.Write("Hello");
>            ILogger log = LogManager.GetLogger("testing blah");  //
> does not work
>            log.Info("{0}","Hello Logger");
>            Console.ReadKey();
>        }
>    }
>
>
>
> Here is the wrapper (yeah the Debug.Writes will go away and be
> replaced with log4net calls in 30 minutes just want to get calling
> syntax for this wrapper now...
>
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using System.Diagnostics;
> using System.IO;
> using log4net;
>
> namespace Test.Logger
> {
>
>    public interface ILogger
>    {
>
>        void Debug(string format, params object[] args);
>        void Info(string format, params object[] args);
>        void Warn(string format, params object[] args);
>        void Error(string format, params object[] args);
>        void Fatal(string format, params object[] args);
>
>        void Debug(string format, Exception exception, params object[] args);
>        void Info(string format, Exception exception, params object[] args);
>        void Warn(string format, Exception exception, params object[] args);
>        void Error(string format, Exception exception, params object[] args);
>        void Fatal(string format, Exception exception, params object[] args);
>
>        bool IsDebugEnabled
>        {
>            get;
>        }
>        bool IsInfoEnabled
>        {
>            get;
>        }
>        bool IsWarnEnabled
>        {
>            get;
>        }
>        bool IsErrorEnabled
>        {
>            get;
>        }
>        bool IsFatalEnabled
>        {
>            get;
>        }
>
>    }
>
>    public sealed class LogManager
>    {
>
>        public static void Configure(FileInfo configFile)
>        {
>
>            if (configFile == null)
>            {
>                throw new System.ArgumentNullException("configFile");
>            }
>
>            System.Diagnostics.EventLog.WriteEntry(string.Format("{0}.LogManager",
> Process.GetCurrentProcess().ProcessName), string.Format("Configuring
> logging using configuration file {0}", configFile.FullName));
>
>            log4net.Config.XmlConfigurator.ConfigureAndWatch(configFile);
>
>
>            GetLogger(typeof(LogManager)).Debug("Configured logging
> using configuration file {0}", configFile.FullName);
>        }
>
>        public static ILogger GetLogger(System.Type type)
>        {
>            log4net.ILog logger =
> log4net.LogManager.GetLogger(System.Reflection.Assembly.GetCallingAssembly(),
> type);
>            return new Log4NetAdapter(logger);
>        }
>
>    }
>
>    internal class Log4NetAdapter : ILogger
>    {
>
>        private ILog _inner;
>
>        internal Log4NetAdapter(ILog log4netLogger)
>        {
>            _inner = log4netLogger;
>        }
>
>        public void Debug(string format, params object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format,args));
>        }
>        public void Info(string format, params object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args));
>        }
>        public void Warn(string format, params object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args));
>        }
>        public void Error(string format, params object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args));
>        }
>        public void Fatal(string format, params object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args));
>        }
>
>        public void Debug(string format, Exception exception, params
> object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args)
> + exception.Message);
>        }
>
>        public void Info(string format, Exception exception, params
> object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args)
> + exception.Message);
>        }
>
>        public void Warn(string format, Exception exception, params
> object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args)
> + exception.Message);
>        }
>
>        public void Error(string format, Exception exception, params
> object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args)
> + exception.Message);
>        }
>
>        public void Fatal(string format, Exception exception, params
> object[] args)
>        {
>            System.Diagnostics.Debug.Write(string.Format(format, args)
> + exception.Message);
>        }
>
>        public bool IsDebugEnabled { get; set; }
>
>        public bool IsInfoEnabled { get; set; }
>
>        public bool IsWarnEnabled { get; set; }
>
>        public bool IsErrorEnabled { get; set; }
>
>        public bool IsFatalEnabled { get; set; }
>
>    } // end Log4NetAdapter
>
> } // end namespace
>