Пример #1
0
        public static IEnumerable <LinLogTemplate> TemplateParser(string template)
        {
            var matches      = Regex.Matches(template, Pattern);
            var segments     = matches.Select(m => m.Value).ToList();
            var logTemplates = new List <LinLogTemplate>();

            foreach (var segment in segments)
            {
                try
                {
                    var logTemplate = new LinLogTemplate(segment);
                    logTemplates.Add(logTemplate);
                }
                catch (Exception)
                {
                    continue;
                }
            }

            return(logTemplates);
        }
Пример #2
0
        private string TemplateReplace(string template)
        {
            var currentUser = (ICurrentUser)_httpContextAccessor.HttpContext.RequestServices.GetService(typeof(ICurrentUser));

            if (currentUser.Username == null)
            {
                return(template);
            }

            var logTemplates = LinLogTemplate.TemplateParser(template);

            var newTemplate = template;

            foreach (var logTemplate in logTemplates)
            {
                if (!_templateObj.Contains(logTemplate.ClassObj))
                {
                    continue;
                }

                string?item;
                var    flag = false;
                switch (logTemplate.ClassObj)
                {
                case "user":
                    var propertyUser = typeof(CurrentUser).GetProperty(logTemplate.Property,
                                                                       BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                    if (propertyUser == null)
                    {
                        flag = true;
                    }
                    item = propertyUser?.GetValue(currentUser)?.ToString();
                    break;

                case "response":
                    var propertyResponse = typeof(HttpResponse).GetProperty(logTemplate.Property,
                                                                            BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                    if (propertyResponse == null)
                    {
                        flag = true;
                    }
                    item = propertyResponse?.GetValue(_httpContextAccessor.HttpContext.Response)?.ToString();
                    break;

                default:
                    var propertyRequest = typeof(HttpRequest).GetProperty(logTemplate.Property,
                                                                          BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                    if (propertyRequest == null)
                    {
                        flag = true;
                    }
                    item = propertyRequest?.GetValue(_httpContextAccessor.HttpContext.Request)?.ToString();
                    break;
                }

                if (flag)
                {
                    continue;
                }

                newTemplate = newTemplate.Replace(logTemplate.Segment, item);
            }

            return(newTemplate);
        }