protected void btnViewIndex_Click(object sender, EventArgs e) { var log_indexs = MongoDataBase.GetCollection <LogEntity>().Indexes.ListAsync(); var log_indexs_result = log_indexs.Result.ToListAsync().Result; foreach (var item in log_indexs_result) { Response.Write(item.ToString()); } var tag_indexs = MongoDataBase.GetCollection <LogTag>().Indexes.ListAsync(); var tag_indexs_result = tag_indexs.Result.ToListAsync().Result; foreach (var item in tag_indexs_result) { Response.Write(item.ToString()); } var statistics_indexs = MongoDataBase.GetCollection <LogTag>().Indexes.ListAsync(); var statistics_indexs_result = statistics_indexs.Result.ToListAsync().Result; foreach (var item in statistics_indexs_result) { Response.Write(item.ToString()); } }
/// <summary> /// 获取配置项 /// </summary> /// <param name="keys"></param> /// <returns></returns> public List <Options> GetOptions(string[] keys) { var collection = MongoDataBase.GetCollection <Options>(); var filterBuilder = Builders <Options> .Filter; var filter = filterBuilder.In("Key", keys); var result = collection.Find(filter).ToListAsync().Result; return(result); }
public LogOnOff GetLogOnOff(int appId) { var filterBuilder = Builders <LogOnOff> .Filter; var filter = filterBuilder.Eq("AppId", appId); var collection = MongoDataBase.GetCollection <LogOnOff>(); var result = collection.Find(filter).FirstOrDefaultAsync().Result; return(result); }
public List <LogOnOff> GetALLLogOnOff() { var filterBuilder = Builders <LogOnOff> .Filter; var filter = filterBuilder.Gt("AppId", 0); var collection = MongoDataBase.GetCollection <LogOnOff>(); var result = collection.Find(filter).ToListAsync <LogOnOff>().Result; return(result.OrderBy(x => x.AppId).ToList()); }
public AlertingHistory GetLastAlertingHistory(int appId, AlertingType type) { var collection = MongoDataBase.GetCollection <AlertingHistory>(); var filterBuilder = Builders <AlertingHistory> .Filter; var filter = filterBuilder.Gt("ObjId", appId) & filterBuilder.Gt("AlertingType", type); var result = collection.Find(filter) .SortByDescending(x => x.Time) .FirstOrDefaultAsync().Result; return(result); }
protected void btnCreateIndex_Click(object sender, EventArgs e) { var options = new CreateIndexOptions() { Unique = false, Background = true }; var field1 = new StringFieldDefinition <LogEntity>("AppId"); var indexDefinition1 = new IndexKeysDefinitionBuilder <LogEntity>().Ascending(field1); MongoDataBase.GetCollection <LogEntity>().Indexes.CreateOneAsync(indexDefinition1, options); var field2 = new StringFieldDefinition <LogEntity>("Level"); var indexDefinition2 = new IndexKeysDefinitionBuilder <LogEntity>().Ascending(field2); MongoDataBase.GetCollection <LogEntity>().Indexes.CreateOneAsync(indexDefinition2, options); var field3 = new StringFieldDefinition <LogEntity>("Time"); var indexDefinition3 = new IndexKeysDefinitionBuilder <LogEntity>().Descending(field3); MongoDataBase.GetCollection <LogEntity>().Indexes.CreateOneAsync(indexDefinition3, options); var field4 = new StringFieldDefinition <LogTag>("Tag"); var indexDefinition4 = new IndexKeysDefinitionBuilder <LogTag>().Ascending(field4); MongoDataBase.GetCollection <LogTag>().Indexes.CreateOneAsync(indexDefinition4, options); var field5 = new StringFieldDefinition <LogTag>("Time"); var indexDefinition5 = new IndexKeysDefinitionBuilder <LogTag>().Descending(field5); MongoDataBase.GetCollection <LogTag>().Indexes.CreateOneAsync(indexDefinition5, options); var field8 = new StringFieldDefinition <LogTag>("LogId"); var indexDefinition8 = new IndexKeysDefinitionBuilder <LogTag>().Ascending(field8); MongoDataBase.GetCollection <LogTag>().Indexes.CreateOneAsync(indexDefinition8, options); var field6 = new StringFieldDefinition <LogStatistics>("Time"); var indexDefinition6 = new IndexKeysDefinitionBuilder <LogStatistics>().Descending(field6); MongoDataBase.GetCollection <LogStatistics>().Indexes.CreateOneAsync(indexDefinition6, options); var field7 = new StringFieldDefinition <LogStatistics>("AppId"); var indexDefinition7 = new IndexKeysDefinitionBuilder <LogStatistics>().Ascending(field7); MongoDataBase.GetCollection <LogStatistics>().Indexes.CreateOneAsync(indexDefinition7, options); Response.Write("Create Index Success"); }
public void SetLogOnOff(LogOnOff on_off) { var collection = MongoDataBase.GetCollection <LogOnOff>(); var old_on_off = collection.Find(a => a.AppId == on_off.AppId).Limit(1).FirstOrDefaultAsync().Result; if (old_on_off == null) { collection.InsertOneAsync(on_off); } else { on_off._id = old_on_off._id; collection.ReplaceOneAsync(a => a.AppId == on_off.AppId, on_off); } }
public List <LogStatistics> GetStatistics(long start, long end, int appId) { // if (limit <= 0) { limit = 100; } var filterBuilder = Builders <LogStatistics> .Filter; var filter = filterBuilder.Gte("Time", start) & filterBuilder.Lt("Time", end); if (appId > 0) { filter = filter & filterBuilder.Eq("AppId", appId); } var collection = MongoDataBase.GetCollection <LogStatistics>(); var s = collection.Find(filter).ToListAsync <LogStatistics>().Result; var appIds = s.Select(x => x.AppId).Distinct(); List <LogStatistics> result = new List <LogStatistics>(); var logOnOffs = GetALLLogOnOff(); foreach (var _appId in appIds) { var lst = s.Where(x => x.AppId == _appId); var onOff = logOnOffs.FirstOrDefault(x => x.AppId == _appId); LogStatistics item = new LogStatistics(); item.AppId = _appId; item.Debug = lst.Sum(x => x.Debug); item.Info = lst.Sum(x => x.Info); item.Warm = lst.Sum(x => x.Warm); item.Error = lst.Sum(x => x.Error); if (onOff != null) { item.AppName = onOff.AppName; } result.Add(item); } return(result .OrderByDescending(x => x.Error) .ThenByDescending(x => x.Warm) .ThenByDescending(x => x.Info) .ThenByDescending(x => x.Debug) .ToList()); }
public void SetOptions(List <Options> lst) { var log_ls_collection = MongoDataBase.GetCollection <Options>(); foreach (var item in lst) { var filterBuilder = Builders <Options> .Filter; var filter = filterBuilder.Eq("Key", item.Key); var opt = log_ls_collection.Find(filter).FirstOrDefault(); if (opt == null) { log_ls_collection.InsertOneAsync(item); } else { var updateBuilder = Builders <Options> .Update; var update = updateBuilder.Set("Value", item.Value) .Set("UpdateTime", item.UpdateTime); log_ls_collection.UpdateOneAsync(filter, update); } } }
public void Write(IList <LogEntity> logs) { #region 写日志主体 var log_collection = MongoDataBase.GetCollection <LogEntity>(); log_collection.InsertManyAsync(logs); #endregion #region 写Tag List <LogTag> tags = new List <LogTag>(); foreach (var log in logs) { if (log.Tags != null) { foreach (var tag in log.Tags) { tags.Add(new LogTag { LogId = log._id, Tag = Utils.BKDRHash(tag), Time = log.Time }); } } } if (tags.Count > 0) { var tag_collection = MongoDataBase.GetCollection <LogTag>(); tag_collection.InsertManyAsync(tags); //foreach (var item in tags) //{ // var result = tag_collection.InsertOneAsync(item); //} } #endregion List <LogStatistics> lss = new List <LogStatistics>(); var app_grp = from a in logs group a by a.AppId into g select g; long statisticsTime = Utils.GetTimeStamp(DateTime.Now); foreach (var grp in app_grp) { int debug = grp.Count(x => x.Level == LogLevel.Debug); int info = grp.Count(x => x.Level == LogLevel.Info); int warn = grp.Count(x => x.Level == LogLevel.Warn); int error = grp.Count(x => x.Level == LogLevel.Error); LogStatistics ls = new LogStatistics(); ls.AppId = grp.Key; ls.Debug = debug; ls.Info = info; ls.Warn = warn; ls.Error = error; ls.Time = statisticsTime; lss.Add(ls); } var log_ls_collection = MongoDataBase.GetCollection <LogStatistics>(); log_ls_collection.InsertManyAsync(lss); }
public void RecordAlerting(AlertingHistory alert) { var log_ls_collection = MongoDataBase.GetCollection <AlertingHistory>(); log_ls_collection.InsertOneAsync(alert); }
private List <LogEntity> GetLogsNoTags(long start, long end, int appId, int[] level, string title, string msg, string source, long ip, int limit = 100) { if (limit <= 0) { limit = 100; } var filterBuilder = Builders <LogEntity> .Filter; var filter = filterBuilder.Gte("Time", start) & filterBuilder.Lt("Time", end); if (level != null & level.Length > 0) { if (level.Length == 1) { filter = filter & filterBuilder.Eq("Level", level[0]); } else { Array.Sort(level); if (string.Join(",", level) != "1,2,3,4") { filter = filter & filterBuilder.In <int>("Level", level); } } } if (appId > 0) { filter = filter & filterBuilder.Eq("AppId", appId); } if (ip > 0) { filter = filter & filterBuilder.Eq("IP", ip); } if (!string.IsNullOrWhiteSpace(source)) { filter = filter & filterBuilder.Eq("Source", source); } if (!string.IsNullOrWhiteSpace(title)) { var re = new BsonRegularExpression(".*" + title + ".*", "i"); filter = filter & filterBuilder.Regex("Title", re); } if (!string.IsNullOrWhiteSpace(msg)) { var re = new BsonRegularExpression(".*" + msg + ".*", "i"); filter = filter & filterBuilder.Regex("Message", re); } var collection = MongoDataBase.GetCollection <LogEntity>(); var result = collection.Find(filter) .SortByDescending(x => x.Time) .Limit(limit) .ToListAsync <LogEntity>() .Result; return(result); }
private List <LogEntity> GetLogsInTags(long start, long end, int appId, int[] level, string title, string msg, string source, long ip, List <string> tags, int limit = 100) { if (limit <= 0) { limit = 100; } List <long> _tags = new List <long>(); foreach (var tag in tags) { _tags.Add(Utils.BKDRHash(tag)); } var tagFilterBuilder = Builders <LogTag> .Filter; var tagFilter = tagFilterBuilder.Gte("Time", start) & tagFilterBuilder.Lt("Time", end) & tagFilterBuilder.In <long>("Tag", _tags); var tagCollection = MongoDataBase.GetCollection <LogTag>(); var tagEntity = tagCollection.Find(tagFilter).Project(x => x.LogId).ToListAsync().Result; if (tagEntity == null || tagEntity.Count == 0) { return(new List <LogEntity>()); } var filterBuilder = Builders <LogEntity> .Filter; var filter = filterBuilder.Gte("Time", start) & filterBuilder.Lt("Time", end); if (level != null & level.Length > 0) { if (level.Length == 1) { filter = filter & filterBuilder.Eq("Level", level[0]); } else { Array.Sort(level); if (string.Join(",", level) != "1,2,3,4") { filter = filter & filterBuilder.In <int>("Level", level); } } } if (appId > 0) { filter = filter & filterBuilder.Eq("AppId", appId); } if (ip > 0) { filter = filter & filterBuilder.Eq("IP", ip); } if (!string.IsNullOrWhiteSpace(source)) { filter = filter & filterBuilder.Eq("Source", source); } if (tagEntity.Count == 1) { filter = filter & filterBuilder.Eq("_id", tagEntity.First()); } else { filter = filter & filterBuilder.In("_id", tagEntity); } if (!string.IsNullOrWhiteSpace(title)) { var re = new BsonRegularExpression(".*" + title + ".*", "i"); filter = filter & filterBuilder.Regex("Title", re); } if (!string.IsNullOrWhiteSpace(msg)) { var re = new BsonRegularExpression(".*" + msg + ".*", "i"); filter = filter & filterBuilder.Regex("Message", re); } var collection = MongoDataBase.GetCollection <LogEntity>(); var result = collection.Find(filter) .SortByDescending(x => x.Time) .Limit(limit) .ToListAsync <LogEntity>() .Result; return(result); }