using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using XHandler.Class; using XImagingXhandler.XDAL; using System.IO; using XCommon; using XCommon.Log; using XCoreBLL; using XHandler.View.Page; using DataEntity; using DataEntity.Device; using DataEntity.Page; using DataRWDAL; using HxEnum; using NPOI.SS.Formula.Functions; namespace XHandler.View.DataLog { /// /// DataLogList.xaml 的交互逻辑 /// public partial class DataLogList : UserControl { ObservableCollection logInformationList = new ObservableCollection(); LogInformation logInfo = new LogInformation(); public MainWindow mainWindow = null; #region 变量 // 分页数据 private Pagination m_pagination = new Pagination(); #endregion public DataLogList() { InitializeComponent(); } private void btnSearch_Click(object sender, RoutedEventArgs e) { dgResult.ItemsSource = null; pagerToolsControl_paging.Current = PaginationDefaultParameter.Current; } private void btnView_Click(object sender, RoutedEventArgs e) { try { if (EventResponseController.Instance.CanExecute() == false) return; Button btn = (Button)sender; if (btn != null) { string id = (string)btn.Tag; var recordLog = logInformationList.SingleOrDefault(x => x.log_filepath == btn.Tag.ToString()); string strPath = recordLog.log_filepath; if (File.Exists(strPath)) { System.Diagnostics.Process.Start("notepad.exe", strPath); } } } catch (Exception ex) { } } private void UserControl_Loaded(object sender, RoutedEventArgs e) { try { BindingDatalogCollection(); } catch(Exception ex) { } } private void DataGridCell_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { } private void pagerToolsControl_paging_PageCntChanged(object sender, Page.C_EventArgsClass e) { m_pagination.Current = e.CurrentPageNo; DataBinding(m_pagination); } private void pagerToolsControl_paging_OnePageRowCntChanged(object sender, Page.C_EventArgsClass e) { m_pagination.Current = e.CurrentPageNo; m_pagination.PageSize = e.PagerNum; DataBinding(m_pagination); } /// /// 分页绑定 /// /// private void DataBinding(Pagination pagination) { string strFileName = this.tbxDataLogName.Text; logInformationList = GenerateDataLogCollection(strFileName); var resultLogInformationList = GetPagedData(logInformationList, pagination.Current, pagination.PageSize); var result = new Tuple, int>(resultLogInformationList, logInformationList.Count()); if (result != null) { pagerToolsControl_paging.IsTrigger = false; pagerToolsControl_paging.DataCount = result.Item2; // 当前查到数量 pagerToolsControl_paging.Current = pagination.Current; pagerToolsControl_paging.IsTrigger = true; dgResult.ItemsSource = result.Item1; } } #region 读取运行日志记录文件到集合 private ObservableCollection GenerateDataLogCollection(string fileName) { ObservableCollection logInformationArray = new ObservableCollection(); string basePath = System.AppDomain.CurrentDomain.BaseDirectory; string runLogFolderPath = basePath + @"Logs\Handler";//运行操作目录 string connectServeFolderPath = basePath + @"LogFiles";//运行服务目录 string controlRunFolderPath = basePath + @"Logs\DriveRun";//控制运行目录 try { int i = 0; if (Directory.Exists(runLogFolderPath)) { string[] runLogFileInfo = Directory.GetFiles(runLogFolderPath); foreach (string file in runLogFileInfo) { LogInformation logInformation = new LogInformation(); logInformation.log_id = (1).ToString(); logInformation.log_name = file.Substring(file.LastIndexOf('\\') + 1, file.Length - file.LastIndexOf('\\') - 1); logInformation.log_filepath = file; logInformation.log_content = string.Empty; logInformation.log_type = "运行日志"; FileInfo fileInfo = new FileInfo(file); logInformation.time_stamp = fileInfo.LastWriteTime; logInformationArray.Add(logInformation); } } if (Directory.Exists(controlRunFolderPath)) { string[] controlRunFileInfo = Directory.GetFiles(controlRunFolderPath); foreach (string file in controlRunFileInfo) { LogInformation logInformation = new LogInformation(); logInformation.log_id = (1).ToString(); logInformation.log_name = file.Substring(file.LastIndexOf('\\') + 1, file.Length - file.LastIndexOf('\\') - 1); logInformation.log_filepath = file; logInformation.log_content = string.Empty; logInformation.log_type = "动作日志"; FileInfo fileInfo = new FileInfo(file); logInformation.time_stamp = fileInfo.LastWriteTime; logInformationArray.Add(logInformation); } } if (Directory.Exists(connectServeFolderPath)) { string[] connectServeFileInfo = Directory.GetFiles(connectServeFolderPath); foreach (string file in connectServeFileInfo) { LogInformation logInformation = new LogInformation(); logInformation.log_id = (1).ToString(); logInformation.log_name = file.Substring(file.LastIndexOf('\\') + 1, file.Length - file.LastIndexOf('\\') - 1); logInformation.log_filepath = file; logInformation.log_content = string.Empty; logInformation.log_type = "通信日志"; FileInfo fileInfo = new FileInfo(file); logInformation.time_stamp = fileInfo.LastWriteTime; logInformationArray.Add(logInformation); } } if (logInformationArray.Count > 0) { logInformationArray = new ObservableCollection(logInformationArray.OrderByDescending(x => x.time_stamp)); logInformationArray = new ObservableCollection(logInformationArray.Where(x => x.log_name.Contains(fileName)).ToList()); foreach (LogInformation log in logInformationArray) { log.log_id = (++i).ToString(); } } } catch (Exception ex) { } return logInformationArray; } #endregion #region 绑定数据日志集合到界面 private void BindingDatalogCollection() { DataBinding(m_pagination); } #endregion #region 集合数据分页方法 private static IEnumerable GetPagedData(IEnumerable source, int currentPageNumber, int pageSize) { if (source == null || !source.Any()) return Enumerable.Empty(); int startIndex = (currentPageNumber - 1) * pageSize; int endIndex = Math.Min(startIndex + pageSize, source.Count()); return source.Skip(startIndex).Take(endIndex - startIndex); } #endregion private void btnReset_Click(object sender, RoutedEventArgs e) { tbxDataLogName.Text = ""; btnSearch_Click(sender, e); } } }