Beispiel #1
0
        public static bool WriteLog(LogEventTypeEnum evtType, Exception ex, string message, params object[] messageParameters)
        {
            try
            {
                if (logger == null)
                {
                    throw new ApplicationException("Logger.CurrentLogger cannot be null");
                }

                var formattedMessage = Logger.FormatMessage(message, messageParameters);

                logger.Write(evtType, formattedMessage, ex);

                return(true);
            }
            catch (Exception iex)
            {
                try
                {
                    Trace.WriteLine(string.Format("{0} - ({1:yyyy-MM-dd HH:mm:ss}): {2}"
                                                  , evtType
                                                  , DateTime.Now
                                                  , string.Format("Logging Error: '{0}'", iex.ToString())
                                                  ));
                }
                catch
                {
                }

                return(false);
            }
        }
        public void Write(LogEventTypeEnum evtType, string message, Exception ex)
        {
            if (Logger == null)
            {
                Logger = NLog.LogManager.GetLogger("AppDomainLogger");
            }

            var logLevel = LogLevel.Info;

            switch (evtType)
            {
            case LogEventTypeEnum.Warning:
                logLevel = LogLevel.Warn;
                break;

            case LogEventTypeEnum.Error:
                logLevel = LogLevel.Error;
                break;

            case LogEventTypeEnum.Debug:
                logLevel = LogLevel.Debug;
                break;
            }

            var formatProvider = System.Globalization.CultureInfo.InvariantCulture;

            var logEvent = new LogEventInfo(logLevel, Logger.Name, formatProvider, message, null, ex);

            ExtendLogEvent(logEvent);

            Logger.Log(logEvent);
        }
Beispiel #3
0
        private string GetPath(LogEventTypeEnum evtType, bool isErrorPath)
        {
            string loggerPath;

            if (isErrorPath)
            {
                loggerPath = AppSettingsHelper.GetAppSetting("logger.errorpath", false, @"%appdir\Logs\%dat\Errors\Logs.%usr.log");
            }
            else
            {
                loggerPath = AppSettingsHelper.GetAppSetting("logger.path", false, @"%appdir\Logs\%dat\Logs.%usr.log");
            }

            var builder = new StringBuilder(loggerPath);

            builder.Replace("%appdir", AppDomain.CurrentDomain.BaseDirectory);
            builder.Replace("%tempdir", Path.GetTempPath());
            builder.Replace("%usr", this.GetValidPathName(System.Threading.Thread.CurrentPrincipal.Identity.Name));
            builder.Replace("%prt", this.GetValidPathName(evtType.ToString()));
            builder.Replace("%dat", this.GetValidPathName(DateTime.Now.ToString("yyyy-MM-dd")));

            if (loggerPath.IndexOf("%host") != -1)
            {
                builder.Replace("%host", this.GetValidPathName(Environment.MachineName));
            }

            builder.Replace("/", @"\");

            return(builder.ToString());
        }
Beispiel #4
0
        public override void WriteLog(LogEventTypeEnum evtType, string message)
        {
            base.WriteLog(evtType, message);

            this.TrySaveLog(this.GetPath(evtType, false), message);

            if (evtType >= LogEventTypeEnum.Error)
            {
                this.TrySaveLog(this.GetPath(evtType, true), message);
            }
        }
        public void Write(LogEventTypeEnum evtType, string message, Exception ex)
        {
            if (evtType < currentLogLevel)
            {
                return;
            }

            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException("message");
            }

            var parsedMessage = ParseMessage(evtType, message);

            if (ex != null)
            {
                parsedMessage = string.Format("{0}{1}{2}{1}", parsedMessage, System.Environment.NewLine, ex.ToString());
            }

            WriteLog(evtType, parsedMessage);
        }
        private string ParseMessage(LogEventTypeEnum evtType, string message)
        {
            string className = null;
            bool   flag      = false;

            var builder = new StringBuilder(format);

            builder.Replace("%usr", GetCurrentUsername());
            builder.Replace("%prt", evtType.ToString());
            builder.Replace("%dat", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            builder.Replace("%msg", message);
            builder.Replace("%tab", "\t");
            builder.Replace("%nln", "\r\n");

            if (format.IndexOf("%host") != -1)
            {
                builder.Replace("%host", Environment.MachineName);
            }

            builder.Replace("%tid", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

            if (format.IndexOf("%mtd") != -1)
            {
                className = GetClassName();
                flag      = true;
                builder.Replace("%mtd", className);
            }

            if (format.IndexOf("%cls") != -1)
            {
                if (!flag)
                {
                    className = GetClassName();
                }
                builder.Replace("%cls", className);
            }

            return(builder.ToString());
        }
Beispiel #7
0
 public override void WriteLog(LogEventTypeEnum evtType, string message)
 {
     Trace.WriteLine(message);
 }
 public abstract void WriteLog(LogEventTypeEnum evtType, string message);
 public BaseLogger()
 {
     currentLogLevel = GetTraceLevelFromConfig();
     format          = AppSettingsHelper.GetAppSetting("logger.format", false, "[%prt][%tid][%dat][%cls.%mtd][%usr]: %msg ");
 }
        public override void WriteLog(LogEventTypeEnum evtType, string message)
        {
            base.WriteLog(evtType, message);

            Console.WriteLine(message);
        }