private void btnImportNewLogs_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; try { ILogOperator opt = LogContentService.Instance.GetLogOperator(m_app.AppGUID); OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; ofd.Filter = opt.Filter; this.Cursor = Cursors.Default; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.Cursor = Cursors.WaitCursor; // 导入文件后先显示,然后用户确认后再保存到数据库 Dictionary <string, List <LogRecord> > lstLogs = opt.ReadLogFromFiles(m_app.AppGUID, ofd.FileNames); foreach (LogShowTabPage page in m_tabPages.Values) { if (page.Parent == null) { lstLogs.Remove(Convert.ToString(page.Tag)); } } this.Cursor = Cursors.Default; if (lstLogs.Count > 0) { this.Cursor = Cursors.WaitCursor; frmLogViewer flv = new frmLogViewer(m_app, lstLogs); if (CGeneralFuncion.ShowWindow(null, flv, true) == System.Windows.Forms.DialogResult.OK) { this.Cursor = Cursors.WaitCursor; foreach (string tableGuid in lstLogs.Keys) { if (!LogContentService.Instance.SaveAppLogs(m_app.AppGUID, tableGuid, lstLogs[tableGuid])) { throw new Exception("保存日志到数据库失败"); } AddLogs2TabPage(tableGuid, lstLogs[tableGuid]); } } } } } catch (Exception ex) { MessageBox.Show(string.Format("导入{0}的日志时出错,错误消息为:{1}", m_app.Name, ex.Message)); } finally { this.Cursor = Cursors.Default; } }
public void AddLogOperator(string appGuid, ILogOperator opt) { if (m_operators.ContainsKey(appGuid)) { throw new Exception("缓冲区中已经存在该应用程序的操作类!"); } m_operators.Add(appGuid, opt); }
private void LoadLogOperators() { try { // 动态加载日志处理函数 string dirPath = Path.Combine(Application.StartupPath, LogOperatorPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } DirectoryInfo di = new DirectoryInfo(dirPath); FileInfo[] files = di.GetFiles("*" + LogOperatorFileExtension); if (files != null && files.Length > 0) { StringBuilder sbErrors = new StringBuilder(); XmlSerializer dataSerializer = new XmlSerializer(typeof(LogOperatorInfo)); XmlTextReader xmlReader = null; foreach (FileInfo info in files) { try { xmlReader = new XmlTextReader(info.FullName); LogOperatorInfo optInfo = (LogOperatorInfo)dataSerializer.Deserialize(xmlReader); if (string.IsNullOrWhiteSpace(optInfo.AppGuid)) { throw new Exception("应用程序Guid不能为空"); } string assemblyFile = Path.Combine(dirPath, optInfo.FileName); if (m_operators.ContainsKey(optInfo.AppGuid)) { throw new Exception(string.Format("应用程序Guid:{0}已经存在", optInfo.AppGuid)); } if (!File.Exists(assemblyFile)) { throw new Exception(string.Format("文件{0}不存在或者没有权限访问", assemblyFile)); } Assembly optor = Assembly.LoadFile(assemblyFile); foreach (Module m in optor.GetModules()) { foreach (Type t in m.GetTypes()) { Type interfaceT = t.GetInterface("ILogOperator", true); if (interfaceT == null) { continue; } ILogOperator optInstance = (ILogOperator)System.Activator.CreateInstance(t); optInstance.AppGuid = optInfo.AppGuid; optInstance.Filter = optInfo.Filter; m_operators.Add(optInfo.AppGuid, optInstance); } } } catch (Exception ex) { sbErrors.AppendLine(string.Format("文件{0}解析错误,错误消息为:{1}", info.FullName, ex.Message)); } finally { if (xmlReader != null) { xmlReader.Close(); xmlReader = null; } } } if (sbErrors.Length > 0) { throw new Exception(sbErrors.ToString()); } } } catch (Exception ex) { throw new Exception("加载日志操作类失败,错误消息为:" + ex.Message); } }