示例#1
0
 static LogHelper()
 {
     //Configure log4net
     try
     {
         //If repository is already configured, skip this step.
         if (!LogManager.GetLoggerRepository(Assembly.GetExecutingAssembly()).Configured)
         {
             //Look in our assembly same folder or Machine.config'
             DOMConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "log4net.config")));
         }
     }
     catch (Exception e)
     {
         EventLogHelper.WriteLogEntry(e.Message, EventLogEntryType.Warning);
     }
 }
示例#2
0
        private static void PublishInternal(string message, Exception exception, LogEntryType exceptionTpe)
        {
                        #if !POCKETPC
            //Using the StackTrace object may be tricky on release builds because of inlining optimizations.
                        #if !POCKETPC
            StackTrace stackTrace     = new StackTrace();
            MemberInfo prevMethodInfo = (MemberInfo)stackTrace.GetFrame(2).GetMethod();
            Type       callertype     = prevMethodInfo.ReflectedType;
            ILog       Log            = LogHelper.GetLogger(callertype.Assembly, callertype);
                        #else
            Type callertype = typeof(LogHelper);
            ILog Log        = LogHelper.GetLogger(Assembly.GetExecutingAssembly(), callertype);
                        #endif

            switch (exceptionTpe)
            {
            case LogEntryType.Audit:
                if (Log.Logger.IsEnabledFor(Level.NOTICE))
                {
                    Log.Logger.Log(callertype.FullName, Level.NOTICE,
                                   InternalFormattedMessage(message, exception, callertype.Assembly), null);
                }
                else
                //fallback log
                                                #if !POCKETPC
                { EventLogHelper.WriteLogEntry(InternalFormattedMessage(message, exception, callertype.Assembly), EventLogEntryType.SuccessAudit); }
                                                #else
                { System.Diagnostics.Debug.WriteLine(InternalFormattedMessage(message, exception, callertype.Assembly), "SuccessAudit"); }
                                                #endif

                break;

            case LogEntryType.Error:
                if (Log.IsErrorEnabled)
                {
                    Log.Error(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                else
                //fallback log
                                                #if !POCKETPC
                { EventLogHelper.WriteLogEntry(InternalFormattedMessage(message, exception, callertype.Assembly), EventLogEntryType.Error); }
                                                #else
                { System.Diagnostics.Debug.WriteLine(InternalFormattedMessage(message, exception, callertype.Assembly), "Error"); }
                                                #endif
                break;

            case LogEntryType.Fatal:
                if (Log.IsFatalEnabled)
                {
                    Log.Fatal(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                else
                //fallback log
                                                #if !POCKETPC
                { EventLogHelper.WriteLogEntry(InternalFormattedMessage(message, exception, callertype.Assembly), EventLogEntryType.Error); }
                                                #else
                { System.Diagnostics.Debug.WriteLine(InternalFormattedMessage(message, exception, callertype.Assembly), "Error"); }
                                                #endif
                break;

            case LogEntryType.Warning:
                if (Log.IsWarnEnabled)
                {
                    Log.Warn(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                break;

            case LogEntryType.Debug:
                if (Log.IsDebugEnabled)
                {
                    Log.Debug(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                break;

            default:
                if (Log.IsInfoEnabled)
                {
                    Log.Info(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                break;
            }
                        #endif
        }
示例#3
0
        private static void PublishInternal(string message, Exception exception, LogEntryType exceptionTpe)
        {
            //Using the StackTrace object may be tricky on release builds because of inlining optimizations.
            StackTrace stackTrace     = new StackTrace();
            MemberInfo prevMethodInfo = (MemberInfo)stackTrace.GetFrame(2).GetMethod();
            Type       callertype     = prevMethodInfo.ReflectedType;
            ILog       Log            = LogHelper.GetLogger(callertype.Assembly, callertype);

            switch (exceptionTpe)
            {
            case LogEntryType.Audit:
                if (Log.Logger.IsEnabledFor(Level.Notice))
                {
                    Log.Logger.Log(callertype, Level.Notice,
                                   InternalFormattedMessage(message, exception, callertype.Assembly), null);
                }
                else
                {
                    //fallback log
                    EventLogHelper.WriteLogEntry(InternalFormattedMessage(message, exception, callertype.Assembly), EventLogEntryType.SuccessAudit);
                }

                break;

            case LogEntryType.Error:
                if (Log.IsErrorEnabled)
                {
                    Log.Error(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                else
                {
                    //fallback log
                    EventLogHelper.WriteLogEntry(InternalFormattedMessage(message, exception, callertype.Assembly), EventLogEntryType.Error);
                }

                break;

            case LogEntryType.Fatal:
                if (Log.IsFatalEnabled)
                {
                    Log.Fatal(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                else
                {
                    //fallback log
                    EventLogHelper.WriteLogEntry(InternalFormattedMessage(message, exception, callertype.Assembly), EventLogEntryType.Error);
                }
                break;

            case LogEntryType.Warning:
                if (Log.IsWarnEnabled)
                {
                    Log.Warn(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                break;

            case LogEntryType.Debug:
                if (Log.IsDebugEnabled)
                {
                    Log.Debug(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                break;

            default:
                if (Log.IsInfoEnabled)
                {
                    Log.Info(InternalFormattedMessage(message, exception, callertype.Assembly));
                }
                break;
            }
        }