Esempio n. 1
0
        public async Task <bool> RegisterOrUpdate <T>(LogItem <T> item, object id, string memberName, string memberFile) where T : class, ICloneable
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                collectionName = typeof(T).Name.ToLower();
            }

            item.Url    = httpContext.HttpContext.Request.Path.Value;
            item.Method = memberName;
            item.File   = memberFile;

            var log = await GetById <T>(id);

            if (log != null)
            {
                log.History.Add(item);
            }
            else
            {
                log = new LogModel <T>(item.User, item.OldData);
                log.History.Add(item);
            }

            //var message = new HttpRequestMessage(HttpMethod.Put, $"{objectName.ToLower()}/_doc/{id}");
            //message.Content = new StringContent(JsonConvert.SerializeObject(item), Encoding.UTF8, "application/json");
            var result = await MyRetryPolicy.ExecuteAsync(() => ElasticUrl.AllowAnyHttpStatus()
                                                          .AppendPathSegment($"{collectionName}/_doc/{id}")
                                                          .PutJsonAsync(log));

            Debug.WriteLine(result.ReasonPhrase);

            Debug.WriteLine(await result.Content.ReadAsStringAsync());

            return(result.IsSuccessStatusCode);
        }
Esempio n. 2
0
        public async Task <LogModel <T> > GetById <T>(object id) where T : class, ICloneable
        {
            try
            {
                if (string.IsNullOrEmpty(collectionName))
                {
                    collectionName = typeof(T).Name.ToLower();
                }

                LogModel <T> result   = null;
                var          response = await MyRetryPolicy.ExecuteAsync(() => ElasticUrl.AllowAnyHttpStatus()
                                                                         .AppendPathSegment($"{collectionName}/_doc/{id}")
                                                                         .GetAsync());

                if (response.IsSuccessStatusCode)
                {
                    var temp = await response.Content.ReadAsStringAsync();

                    Debug.WriteLine(temp);
                    var data = JsonConvert.DeserializeObject <ElasticModel <LogModel <T> > >(temp);

                    result = data._source;
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        public async Task <bool> Register <T>(LogItem <T> item) where T : class, ICloneable
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                collectionName = typeof(T).Name.ToLower();
            }

            var log = new LogModel <T>(item.User, item.OldData);

            log.History.Add(item);
            var result = await MyRetryPolicy.ExecuteAsync(() => ElasticUrl.AllowAnyHttpStatus()
                                                          .AppendPathSegment($"{collectionName}/_doc")
                                                          .PostJsonAsync(log));

#if DEBUG
            Debug.WriteLine(result.ReasonPhrase);

            Debug.WriteLine(await result.Content.ReadAsStringAsync());
#endif
            return(result.IsSuccessStatusCode);
        }