// POST api/<controller> public HttpResponseMessage Post([FromBody] ReportData reportData) { string method = "ReportController.Post"; SCTracer.Info(method, string.Format("Start. {0}, {1}", reportData.ReporterName, reportData.ReporterHostName)); bool hasHandle = false; try { // 送信されたデータでreport.jsonを更新 string reportPath = HostingEnvironment.MapPath(Common.JSON_FILE_VPATH); Common.CollectDataList data; // ファイル操作のためにミューテックス取る (※ファイルの排他取るだけでもいいのでは?) hasHandle = mutex.WaitOne(); if (File.Exists(reportPath)) { data = JsonConvert.DeserializeObject <Common.CollectDataList>(File.ReadAllText(reportPath)); } else { SCTracer.Info(method, "Not found ." + reportPath); data = new Common.CollectDataList(); } // 古いデータ(ホスト名が一致)があれば更新、なければ新規追加 int oldDataIndex = data.DataList.FindIndex(x => x.Data.ReporterHostName == reportData.ReporterHostName); if (oldDataIndex < 0) { SCTracer.Info(method, "Add new information."); data.DataList.Add(new Common.CollectData() { Data = reportData }); } else { SCTracer.Info(method, "Update old information."); data.DataList[oldDataIndex].UpdateReportData(reportData); } File.WriteAllText(reportPath, JsonConvert.SerializeObject(data)); SCTracer.Info(method, "End."); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (Exception e) { SCTracer.Error(method, "Unexpected Error Occurred."); SCTracer.Exception(method, e); throw; } finally { if (hasHandle) { mutex.ReleaseMutex(); } } }
public MonitorModel(string filePath) { string method = "MonitorModel.MonitorModel"; bool hasHandle = false; string value = System.Configuration.ConfigurationManager.AppSettings["UnknownStatusThresholdMinutes"]; if (!int.TryParse(value, out unknownStatusThresholdMinutes)) { SCTracer.Error(method, "UnknownStatusThresholdMinutes: failed to parse " + value + " to int."); } value = System.Configuration.ConfigurationManager.AppSettings["MonitorSoftwareNames"]; monitorSoftwareNames = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); for (int i = 0; i < monitorSoftwareNames.Count; i++) { monitorSoftwareNames[i] = monitorSoftwareNames[i].Trim(); } try { hasHandle = mutex.WaitOne(); if (File.Exists(filePath)) { data = JsonConvert.DeserializeObject <Common.CollectDataList>(File.ReadAllText(filePath)); } } catch (Exception e) { SCTracer.Exception(method, e); throw; } finally { if (hasHandle) { mutex.ReleaseMutex(); } } }