示例#1
0
        private static LoggerOptionsAttribute GetCallingMethodOptions(string customCategory)
        {
            StackFrame[] frames   = new StackTrace(2).GetFrames();
            int          hashCode = frames.Aggregate(0, (v, a) => v ^ a.GetMethod().GetHashCode() + 13);
            ConcurrentDictionary <MethodBase, LoggerOptionsAttribute> dictionary = attributeCaches.EnsureKeyValue(hashCode);
            LoggerOptionsAttribute attribute = dictionary.EnsureKeyValue(frames[0].GetMethod(), () => GetCallingMethodOptions(dictionary, frames));

            attribute          = attribute.Clone();
            attribute.Category = customCategory ?? attribute.Category ?? "General";
            return(attribute);
        }
示例#2
0
        private static void WriteLog(LoggerEventType eventType, LoggerOptionsAttribute options, string message)
        {
            LoggerTarget targets = options.GetTargets(eventType);

            if (targets.HasFlag(LoggerTarget.Console))
            {
                ConsoleColor originalColor = Console.ForegroundColor;
                try {
                    Console.ForegroundColor = GetConsoleColor(eventType);
                    Console.WriteLine(message);
                } finally {
                    Console.ForegroundColor = originalColor;
                }
            }
            if (targets.HasFlag(LoggerTarget.EntLib))
            {
                try {
                    LogEntry entry = new LogEntry();
                    entry.Message  = message;
                    entry.Priority = 10 - (int)eventType;
                    entry.Severity = GetTraceEventType(eventType);
                    entry.EventId  = -1;
                    if (!String.IsNullOrEmpty(options.Category))
                    {
                        entry.Categories.Add(options.Category);
                    }
                    EntLibLogger.Write(entry);
                } catch { }
            }
            if (targets.HasFlag(LoggerTarget.UlsLog))
            {
                SPDiagnosticsCategory dCat = new SPDiagnosticsCategory(options.Category, GetTraceSeverity(eventType), GetEventSeverity(eventType));
                SPDiagnosticsService.Local.WriteTrace(0, dCat, GetTraceSeverity(eventType), message);
            }
            if (targets.HasFlag(LoggerTarget.EventLog))
            {
                if (!EventLog.SourceExists(options.Category))
                {
                    EventLog.CreateEventSource(options.Category, options.Category);
                    while (!EventLog.SourceExists(options.Category))
                    {
                        Thread.Sleep(100);
                    }
                }
                EventLog.WriteEntry(options.Category, message, GetEventLogEntryType(eventType), 0);
            }
        }