private static IndexWriter GetWriter(String project) { Remove(); if (String.IsNullOrWhiteSpace(project)) { project = "NoneName"; } String path = LoggerModel.Getpath(project, DateTime.Now); if (m_indexWrite.ContainsKey(project)) { Interlocked.Exchange(ref m_update, 1); return(m_indexWrite[project]); } lock (m_lock) { if (m_indexWrite.ContainsKey(project)) { Interlocked.Exchange(ref m_update, 1); return(m_indexWrite[project]); } IndexWriter fsWriter = null; Boolean isExiested = File.Exists(Path.Combine(path, "write.lock")); FSDirectory fsDir = FSDirectory.Open(new DirectoryInfo(path)); Analyzer analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); //Analyzer analyser = new PanGuAnalyzer(); fsWriter = new IndexWriter(fsDir, analyser, !isExiested, IndexWriter.MaxFieldLength.UNLIMITED); m_indexWrite.TryAdd(project, fsWriter); return(fsWriter); } }
public static IServiceCollection AddMyService(this IServiceCollection services, IConfiguration Configuration) { LoggerModel.SetLogger(Configuration); //MQRabbitConfig.SetConfig(Configuration); //日志的MQ接受消息,每次MQ的通信1m速度大概是40ms,消费能够达到10次/1ms, //那么在执行期间最好控制在800条以下能够控制MQ消费 // 注意只测试了1G数量的日志,再多没测试,理论会逐渐下降, //mq消息队列,配置时候展示,用的时候直接使用 //services.AddHostedService<LoggerMqConsume>(); return(services); }
private static IndexSearcher GetSearcher(String project, DateTime dt) { Remove(); if (String.IsNullOrWhiteSpace(project)) { project = "NoneName"; } String path = LoggerModel.Getpath(project, dt); if (m_indexSearch.ContainsKey(project)) { var cacheIndex = m_indexSearch[project]; if (cacheIndex.ContainsKey(path)) { if (Interlocked.CompareExchange(ref m_update, 0, 1) == 1 && dt.Day == m_now.Day) { cacheIndex.TryRemove(path, out var re); } else { return(cacheIndex[path]); } } } lock (m_lock) { if (!m_indexSearch.ContainsKey(project)) { m_indexSearch.TryAdd(project, new ConcurrentDictionary <string, IndexSearcher>()); } var cacheIndex = m_indexSearch[project]; if (cacheIndex.ContainsKey(path)) { return(cacheIndex[path]); } bool ReadOnly = true; FSDirectory fsDir = FSDirectory.Open(new DirectoryInfo(path)); IndexSearcher searcher = new IndexSearcher(IndexReader.Open(fsDir, ReadOnly)); cacheIndex.TryAdd(path, searcher); return(searcher); } }
/// <summary> /// 搜索日志 /// </summary> /// <param name="project"></param> /// <param name="str"></param> /// <returns></returns> public static List <String> SearchData(String project, String str, DateTime dt) { if (String.IsNullOrWhiteSpace(str)) { return(null); } String path = LoggerModel.Getpath(project, dt); if (!File.Exists(Path.Combine(path, "write.lock"))) { return(null); } List <String> list = new List <String>(); try { IndexSearcher searcher = GetSearcher(project, dt); bool InOrder = true; ScoreDoc[] scoreDoc = SearchTime(searcher, str, "Content", 10, InOrder); foreach (var docs in scoreDoc) { Document doc = searcher.Doc(docs.Doc); String result = doc.Get("Content"); if (!String.IsNullOrWhiteSpace(result)) { list.Add(result); } } searcher.Dispose(); } catch (Exception e) { LogHelper.Critical("日志查询报错" + e.Message); } return(list); }