protected virtual void WriteCreateEntryException(LogLevel originalLevel, object originalMessage, Exception originalException, Exception createEntryException) { if (null == createEntryException) { throw new ArgumentNullException("createEntryException"); } try { ILogEntry entry = new LogEntry(); if (originalMessage != null) { if (originalMessage is ILogEntry) { entry = (ILogEntry)originalMessage; } else if (originalMessage is IDictionary) { var items = (IDictionary)originalMessage; foreach (DictionaryEntry item in items) { entry.ExtendInfo.Add(item.Key.ToString(), item.Value); } } else { entry.Message = originalMessage.ToString(); } } entry.AppName = AppInstance.Config.AppName; entry.LogName = this.Name; entry.LogTime = DateTime.Now; entry.ServerIP = this.ServerIP; entry.BizType = CreateEntryExceptionBizType; if (originalException != null) { entry.Exception = originalException; entry.Message = string.Format("{0}\r\n[{1}]{2}", entry.Message, entry.BizType, originalException.Message); } else { entry.Message = string.Format("{0}\r\n[{1}]{2}", entry.Message, entry.BizType, createEntryException.Message); } entry.LogLevel = LogLevel.Error; entry.ExtendInfo[OriginalLogLevel] = originalLevel; entry.ExtendInfo[CreateEntryExceptionBizType] = createEntryException; this.WriteMessage(entry); } catch { if (null != originalException) { EffectiveFileLogger.WriteException(originalException); } EffectiveFileLogger.WriteException(createEntryException); } }
protected void WriteMessage(ILogEntry entry) { lock (SyncObject) { if (this.EnabledAsync) { entry.WriteTime = DateTime.Now; var syncCount = 0; this.Listeners.ForEach(item => { if (item.SupportAsync == false && this.IsWriteForListener(entry.LogLevel, this.ListenerIndexes[item.Name])) { try { item.WriteMessage(entry); } catch (Exception ex) { EffectiveFileLogger.WriteException(ex); } syncCount++; } }); if (this.Listeners.Count > syncCount) { this.WriteAsyncMessage(entry); } } else { entry.WriteTime = DateTime.Now; this.Listeners.ForEach(item => { if (this.IsWriteForListener(entry.LogLevel, this.ListenerIndexes[item.Name])) { try { item.WriteMessage(entry); } catch (Exception ex) { EffectiveFileLogger.WriteException(ex); } } }); } } }
protected virtual void WriteAsyncMessageByThread(ILogEntry entry) { if (null == entry) { throw new ArgumentNullException("entry"); } entry.WriteTime = DateTime.Now; this.Listeners.ForEach(item => { if (item.SupportAsync && this.IsWriteForListener(entry.LogLevel, this.ListenerIndexes[item.Name])) { try { item.WriteMessage(entry); } catch (Exception ex) { EffectiveFileLogger.WriteException(ex); } } }); }
void AppendSysInfo(ILogEntry entry) { HttpRequest httpRequest = null; if (string.IsNullOrEmpty(entry.URI) && HttpContext.Current != null) { try { httpRequest = HttpContext.Current.Request; } catch { httpRequest = null; } } if (null != httpRequest) { entry.URI = string.Format("{0}:{1}", httpRequest.HttpMethod, httpRequest.FilePath); } if ((this.LevelLimit.SysInfoLimit & entry.LogLevel) != entry.LogLevel) { return; } if (entry.SysInfo == null) { try { entry.SysInfo = SystemInfo.GetInfo(); } catch (Exception ex) { EffectiveFileLogger.WriteException(ex); } } if (httpRequest == null) { return; } if (entry.ExtendInfo.ContainsKey(ConstLogKeys.HttpRequestKey) == false) { var requestInfo = new Dictionary <string, object>(); if (httpRequest.UrlReferrer != null) { requestInfo.Add("Referrer", httpRequest.UrlReferrer.ToString()); } requestInfo.Add("URL", httpRequest.Url.ToString()); requestInfo.Add("RawUrl", httpRequest.RawUrl); if (httpRequest.Form.Count > 0) { var formInfo = new Dictionary <string, object>(httpRequest.Form.Count); httpRequest.Form.AllKeys.ToList <string>().ForEach(item => { formInfo.Add(item, httpRequest.Form[item]); }); requestInfo.Add("FormParams", formInfo); } if (httpRequest.Cookies.Count > 0) { var cookiesInfo = new Dictionary <string, object>(httpRequest.Cookies.Count); httpRequest.Cookies.AllKeys.ToList <string>().ForEach(item => { var httpCookie = httpRequest.Cookies[item]; var cookieInfo = new HttpCookieInfo(httpCookie); var cookieKey = string.Format("{0}.{1}", httpCookie.Domain, item); cookiesInfo[cookieKey] = cookieInfo; }); requestInfo.Add("Cookies", cookiesInfo); } if (httpRequest.ServerVariables.Count > 0) { var serverInfo = new Dictionary <string, object>(httpRequest.ServerVariables.Count); httpRequest.ServerVariables.AllKeys.ToList <string>().ForEach(item => { var val = httpRequest.ServerVariables[item]; if (string.IsNullOrEmpty(val) == false) { serverInfo[item] = httpRequest.ServerVariables[item]; } }); requestInfo.Add("ServerVariables", serverInfo); } try { var browserInfo = new HttpBrowserInfo(httpRequest.Browser); requestInfo.Add("BrowserInfo", browserInfo); } catch (Exception ex) { entry.ExtendInfo.Add("BrowserInfo-Exception", ex); } entry.ExtendInfo.Add(ConstLogKeys.HttpRequestKey, requestInfo); } }