[ create a new paste ] login | about

Link: http://codepad.org/q1RHXL8r    [ raw code | fork ]

C, pasted on Oct 23:
namespace TestLogging
{
    static class LogWrapper
    {
        private static bool _isInit = false;
        private static Logger _logger = null;
        private static object _locker = new object();

        public static bool IsInited()
        {
            lock (_locker)
            {
                return _isInit;
            }
        }

        public static void LogInit(string basedir)
        {

            lock (_locker)
            {
                // Make sure logger is initializes only once 
                if (!_isInit)
                {
                    var config = new LoggingConfiguration();

                    // Create target as file
                    var fileTarget = new FileTarget();
                    config.AddTarget("file", fileTarget);

                    // Set target properties 
                    fileTarget.Layout = @"${date:format=HH\:mm\:ss} - ${message}";
                    fileTarget.FileName = basedir + "${date:format=yyyy-MM-dd}.log";

                    var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
                    config.LoggingRules.Add(rule2);

                    // Activate the configuration
                    LogManager.Configuration = config;

                    // Get logger instance
                    _logger = LogManager.GetLogger("Example");

                    _isInit = true;
                }
            }
        }

        public static void Log(string s)
        {
            lock (_locker)
            {
                if (_isInit == false)
                    throw new Exception("Not initialized");

                _logger.Error(s);
            }

        }
    }
}


Create a new paste based on this one


Comments: