public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { var strUserInfo = _commonLogLocalEnvInfoGeneratorService.GenerateUserInfo(); var strParentUserInfo = _commonLogLocalEnvInfoGeneratorService.GenerateParentUserInfo(); if (strUserInfo == null) { strUserInfo = string.Empty; } if (strParentUserInfo == null) { strParentUserInfo = string.Empty; } if (state is string) { CommonLog log = null; try { log = new CommonLog() { ID = Guid.NewGuid(), Level = (int)logLevel, ParentID = Guid.Empty, ParentActionName = string.Empty, PreLevelID = Guid.Empty, CurrentLevelID = Guid.Empty, ActionName = string.Empty, RequestBody = string.Empty, ResponseBody = string.Empty, RequestUri = string.Empty, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = true, Message = state as string }; } catch { } Task.Run(async() => { if (log != null) { await log.AddLocal(); } }); } else if (state is CommonLogLocalContent) { var logContent = state as CommonLogLocalContent; CommonLog log = null; try { log = new CommonLog() { ID = Guid.NewGuid(), Level = (int)logLevel, ParentID = Guid.Empty, ParentActionName = string.Empty, PreLevelID = Guid.Empty, CurrentLevelID = Guid.Empty, ActionName = logContent.ActionName, RequestBody = logContent.RequestBody, ResponseBody = logContent.ResponseBody, RequestUri = logContent.RequestUri, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = true, Message = logContent.Message }; } catch { } Task.Run(async() => { if (log != null) { await log.AddLocal(); } }); } else { if (formatter != null) { CommonLog log = null; try { log = new CommonLog() { ID = Guid.NewGuid(), Level = (int)logLevel, ParentID = Guid.Empty, ParentActionName = string.Empty, PreLevelID = Guid.Empty, CurrentLevelID = Guid.Empty, ActionName = string.Empty, RequestBody = string.Empty, RequestUri = string.Empty, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = true, Message = formatter(state, exception) }; } catch { } Task.Run(async() => { if (log != null) { await log.AddLocal(); } }); } } }
public async Task AddLocal(CommonLog log) { await _commonLogStore.AddLocal(log); }
public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { var strUserInfo = _commonLogEnvInfoGeneratorService.GenerateUserInfo(); var strParentUserInfo = _commonLogEnvInfoGeneratorService.GetParentUserInfo(); var pID = _commonLogEnvInfoGeneratorService.GetParentID(); var pActionName = _commonLogEnvInfoGeneratorService.GetParentActionName(); var preID = _commonLogEnvInfoGeneratorService.GetPreLevelID(); var currentID = _commonLogEnvInfoGeneratorService.GetCurrentLevelID(); Guid id = Guid.NewGuid(); Guid parentID = Guid.Empty; string strPActionName = string.Empty; bool root = true; if (pID != null) { root = false; parentID = pID.Value; if (pActionName != null) { strPActionName = pActionName; } } Guid preLevelID = Guid.Empty; if (preID != null) { preLevelID = preID.Value; } Guid currentLevelID = Guid.Empty; if (currentID != null) { currentLevelID = currentID.Value; } if (strUserInfo == null) { strUserInfo = string.Empty; } if (strParentUserInfo == null) { strParentUserInfo = string.Empty; } if (state is string) { CommonLog log = null; try { log = new CommonLog() { ID = id, Level = (int)logLevel, ParentID = parentID, ParentActionName = strPActionName, PreLevelID = preLevelID, CurrentLevelID = currentLevelID, ActionName = string.Empty, RequestBody = string.Empty, ResponseBody = string.Empty, RequestUri = string.Empty, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = root, Message = state as string }; } catch { } Task.Run(async() => { if (log != null) { await _commonLogExecute.Execute(log); } }); } else if (state is CommonLogContent) { var logContent = state as CommonLogContent; if (logContent.ParentID != null) { parentID = logContent.ParentID.Value; } if (logContent.ParentActionName != null) { strPActionName = logContent.ParentActionName; } CommonLog log = null; try { log = new CommonLog() { ID = id, Level = (int)logLevel, ParentID = parentID, ParentActionName = strPActionName, PreLevelID = preLevelID, CurrentLevelID = currentLevelID, ActionName = logContent.ActionName, RequestBody = logContent.RequestBody, ResponseBody = logContent.ResponseBody, RequestUri = logContent.RequestUri, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = root, Message = logContent.Message }; } catch { } Task.Run(async() => { if (log != null) { await _commonLogExecute.Execute(log); } }); } else if (state is CommonLogRootContent) { var logContent = state as CommonLogContent; if (logContent.ParentID != null) { parentID = logContent.ParentID.Value; } if (logContent.ParentActionName != null) { strPActionName = logContent.ParentActionName; } CommonLog log = null; try { log = new CommonLog() { ID = id, Level = (int)logLevel, ParentID = parentID, ParentActionName = strPActionName, PreLevelID = preLevelID, CurrentLevelID = currentLevelID, ActionName = logContent.ActionName, RequestBody = logContent.RequestBody, ResponseBody = logContent.ResponseBody, RequestUri = logContent.RequestUri, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = true, Message = logContent.Message }; } catch { } Task.Run(async() => { if (log != null) { await _commonLogExecute.Execute(log); } }); } else { if (formatter != null) { CommonLog log = null; try { log = new CommonLog() { ID = id, Level = (int)logLevel, ParentID = parentID, ParentActionName = strPActionName, PreLevelID = preLevelID, CurrentLevelID = currentLevelID, ActionName = string.Empty, RequestBody = string.Empty, RequestUri = string.Empty, ContextInfo = strUserInfo, ParentContextInfo = strParentUserInfo, Root = root, Message = formatter(state, exception) }; } catch { } Task.Run(async() => { if (log != null) { await _commonLogExecute.Execute(log); } }); } } }