如何使Log4Net包装器类成为单例类? [英] How to make Log4Net wrapper class as a singleton class?

查看:161
本文介绍了如何使Log4Net包装器类成为单例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个log4net包装器类...但是,当我从其他类调用它来记录错误时,我需要实例化.我需要克服这一点..最近我遇到了一个我不熟悉的单例类..因此,在将当前的包装器类转换为单例类时,我需要帮助.

I have a log4net wrapper class... But i need to instantiate everytime when i call it from other classes to log the error. I need to overcome that.. Recently i came across the singleton class which is not familiar to me.. Hence I need help in converting my current wrapper class to the singleton class..

我正在下面发布我当前正在使用的log4net包装器类.

I am posting my log4net wrapper class which i am currently using below..

using System;
using System.Data;
using System.Configuration;
using Asset.Business;

/// <summary>
/// Summary description for Logger
/// </summary>

namespace AssetsDataService
{
    public class ErrorLogger
    {
        private static log4net.ILog logger = null;
        public ErrorLogger()
        {
            if (logger == null)
            {

                string logConfigPath = ConfigSettings.GetEnvConfigValue("LogConfigXMLPath"); // this contains the path of the xml

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(logConfigPath);
                log4net.Config.DOMConfigurator.Configure(fileInfo);

                string loggerName = ConfigurationManager.AppSettings.Get("ErrorLoggerName"); // this contains the name of the logger class

                logger = log4net.LogManager.GetLogger(loggerName);
            }

        }

        public void Fatal(Object message)
        {
            logger.Fatal(message);
        }

        public void Fatal(Object message, Exception exception)
        {
            logger.Fatal(message, exception);
        }

        public void Error(Object message)
        {
            logger.Error(message);
        }

        public void Error(Object message, Exception exception)
        {
            logger.Error(message, exception);

        }

        public void Debug(Object message)
        {

            logger.Debug(message);
        }

        public void Info(Object message)
        {
            logger.Info(message);
        }
    }
}

这是我试图使包装器类单例的代码:

And this is the code which i tried to make my wrapper class singleton:

using System;
using System.Data;
using System.Configuration;
using Asset.Business;

/// <summary>
/// Summary description for Logger
/// </summary>

namespace AssetsDataService
{
    public class ErrorLogger
    {

        private static volatile ErrorLogger instance;
        private static object syncRoot = new Object();
         private static log4net.ILog logger = null;

        private ErrorLogger()
        {
            if (logger == null)
            {

                string logConfigPath = ConfigSettings.GetEnvConfigValue("LogConfigXMLPath"); // this contains the path of the xml

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(logConfigPath);
                log4net.Config.DOMConfigurator.Configure(fileInfo);

                string loggerName = ConfigurationManager.AppSettings.Get("ErrorLoggerName"); // this contains the name of the logger class

                logger = log4net.LogManager.GetLogger(loggerName);
            }

        }

        public static ErrorLogger Instance()
        {

            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new ErrorLogger();
                }
            }

            return instance;
         }


        public void Fatal(Object message)
        {
            logger.Fatal(message);
        }

        public void Fatal(Object message, Exception exception)
        {
            logger.Fatal(message, exception);
        }

        public void Error(Object message)
        {
            logger.Error(message);
        }

        public void Error(Object message, Exception exception)
        {
            logger.Error(message, exception);

        }

        public void Debug(Object message)
        {

            logger.Debug(message);
        }

        public void Info(Object message)
        {
            logger.Info(message);
        }
    }
}

该类是否是正确的单例类,它将正确处理日志记录吗?

Is this class a correct singleton class and will it handle logging correctly?

我将如何调用记录器ErrorLogger类来记录错误或信息等.

How would i call the logger ErrorLogger class to log an error or info etc..

通过使用我的普通班级,我以前将其称为

By using my normal class i used to call it as

ErrorLogger log = new ErrorLogger();
log.Error(string.Concat("Exception Occurred :" + ex.Message, "/n", ex.StackTrace));

如果我使用单例课程,该如何登录?

How can i log if i use the singleton class??

推荐答案

这是单例模式的正确实现.您可以使用以下语句调用方法进行记录:

This is a correct implementation of the singleton pattern. You can call the methods to log with the following statement:

ErrorLogger.Instance.Fatal(Exception);

请注意,此实现不是线程安全的,这意味着如果您使用多个线程来使用此记录器记录不同的消息,则可能会遇到一些意外的异常.您可以通过用锁将所有公共方法包围起来,从而快速而又轻松地解决此问题.

Please notice that this implementation isn't thread safe, this means if you use several threads to log different messages with this logger you might get some unexpected exceptions. You can fix this quick and dirty by surrounding all your public methods with a lock.

例如:

private object _lock = new object();

public void Error(Object message)
{
    lock(_lock){
        logger.Error(message);
    }
}

还要确保在类内的每个公共方法上使用相同的锁对象.

also make sure you use the same lock object on every public method inside the class.

这篇关于如何使Log4Net包装器类成为单例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆