JobLogDataSet.JobLogDataTable t = new JobLogDataSet.JobLogDataTable(); // 每天一个日志文件(.XML 文件),日志的文件名称为:JobLog yyyy-MM-dd.xml string jobLogFile = AppDomain.CurrentDomain.BaseDirectory + "JobLog " + DateTime.Today.ToString("yyyy-MM-dd") + ".xml"; if (!File.Exists(jobLogFile)) t.WriteXml(jobLogFile); // 从 .XML 文件中读取日志 t.ReadXml(jobLogFile); // 添加一条日志 JobLogDataSet.JobLogRow r = t.NewJobLogRow(); r.TraceLevel = logType.ToString(); r.User = user; r.Datetime = DateTime.Now; r.Module = module; r.Function = function; r.Message = message; t.AddJobLogRow(r); // 保存到日志到 XML 文件 t.WriteXml(jobLogFile); } catch (Exception) {} } 3. 读日志的方法 /// <summary> /// 读日志 /// </summary> /// <returns>返回读取日志的 DataTable</returns> public static JobLogDataSet.JobLogDataTable ReadLog() { JobLogDataSet.JobLogDataTable jobLogDataTable = new JobLogDataSet.JobLogDataTable(); try { // 从应用程序文件夹中,获得所有日志文件 JobLog*.xml string[] jobLogFiles = Directory.GetFiles( AppDomain.CurrentDomain.BaseDirectory, "JobLog*.xml", SearchOption.TopDirectoryOnly); |