public async Task <Dictionary <string, string> > Generate()
        {
            var parentID          = ContextContainer.GetValue <Guid>(ContextExtensionTypes.ParentCommonLogID);
            var parentActionName  = ContextContainer.GetValue <string>(ContextExtensionTypes.ParentCommonLogActionName);
            var preLevelID        = ContextContainer.GetValue <Guid>(ContextExtensionTypes.CurrentCommonLogLevelID);
            var parentContextInfo = _commonLogEnvInfoGeneratorService.GenerateUserInfo();

            ParentLogInfo info = new ParentLogInfo()
            {
                ParentID         = parentID,
                PreLevelID       = preLevelID,
                ParentActionName = parentActionName,
                ContextInfo      = parentContextInfo
            };

            Dictionary <string, string> result = new Dictionary <string, string>()
            {
                { HttpHeaderNames.LogInfo, JsonSerializerHelper.Serializer(info) }
            };

            return(await Task.FromResult(result));
        }
Esempio n. 2
0
        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);
                        }
                    });
                }
            }
        }