• R/O
  • SSH
  • HTTPS

ewatch: Commit


Commit MetaInfo

Revision90 (tree)
Time2018-03-12 22:16:06
Authorbananajinn

Log Message

.NET4.0 → .NET4.5.1
アプリケーションとサービスログのイベントログも拾えるようにする

Change Summary

Incremental Difference

--- trunk/EWatch/EWatch/Watchers/EventLogWatcher.cs (revision 89)
+++ trunk/EWatch/EWatch/Watchers/EventLogWatcher.cs (revision 90)
@@ -9,6 +9,7 @@
99 using System;
1010 using System.Collections.Generic;
1111 using System.Diagnostics;
12+using System.Diagnostics.Eventing.Reader;
1213 using EWatch.Models;
1314 using EWatch.Models.Configurations;
1415
@@ -20,63 +21,74 @@
2021 public class EventLogWatcher : BaseWatcher
2122 {
2223 private string name;
23- private EventLog eventLog;
24- private EntryWrittenEventHandler _handler;
24+ private EventBookmark bookmark;
2525
26- public EventLogWatcher(string name, List<ActionTable> actionTable) : base(actionTable)
26+ public EventLogWatcher(string name, List<ActionTable> actionTable)
27+ : base(actionTable)
2728 {
2829 this.name = name;
29- eventLog = new EventLog(name);
30- _handler = new EntryWrittenEventHandler(EntryWritten);
31- eventLog.EntryWritten += _handler;
32- eventLog.EnableRaisingEvents = true;
3330 }
3431
3532 public override void Watch()
3633 {
34+ using (var reader = new EventLogReader(name)) {
35+ if (bookmark != null) {
36+ reader.Seek(bookmark, 1);
37+ } else {
38+ reader.Seek(System.IO.SeekOrigin.End, 0);
39+ var record = reader.ReadEvent();
40+ if(record != null)
41+ bookmark = record.Bookmark;
42+ }
43+
44+ while (true) {
45+ var record = reader.ReadEvent();
46+ if (record == null)
47+ break;
48+ bookmark = record.Bookmark;
49+ EntryWritten(record);
50+ }
51+ }
3752 }
3853
3954 public override void Dispose()
4055 {
41- eventLog.EntryWritten -= _handler;
4256 }
4357
44- private void EntryWritten(object sender, EntryWrittenEventArgs e)
58+ private void EntryWritten(EventRecord rec)
4559 {
46- var span = DateTime.Now - e.Entry.TimeGenerated;
47- if(span.TotalMinutes > 5){
48- log.InfoFormat("ignored: time={0:yyyy-MM-dd HH:mm:ss}, source={1}",
49- e.Entry.TimeGenerated, e.Entry.Source);
50- return;
60+ if (rec.TimeCreated != null) {
61+ var span = DateTime.Now - rec.TimeCreated.Value;
62+ if (span.TotalMinutes > 5) {
63+ log.InfoFormat("ignored: time={0:yyyy-MM-dd HH:mm:ss}, source={1}",
64+ rec.TimeCreated, rec.ProviderName);
65+ return;
66+ }
67+ string level = "I";
68+ if (rec.Level != null) {
69+ switch (rec.Level.Value) {
70+ case (byte)StandardEventLevel.Error:
71+ level = "E";
72+ break;
73+ case (byte)StandardEventLevel.Warning:
74+ level = "W";
75+ break;
76+ case (byte)StandardEventLevel.Informational:
77+ level = "I";
78+ break;
79+ }
80+ }
81+ if (rec.TimeCreated > DateTime.Now.AddDays(-1)) {
82+ FindAndExecute(new LogData {
83+ Code = rec.Id,
84+ HostName = rec.MachineName,
85+ Source = rec.ProviderName,
86+ Level = level,
87+ TimeStamp = rec.TimeCreated,
88+ Message = rec.FormatDescription(),
89+ });
90+ }
5191 }
52-
53- string level = null;
54- switch(e.Entry.EntryType){
55- case EventLogEntryType.Error:
56- level = "E";
57- break;
58- case EventLogEntryType.Warning:
59- case EventLogEntryType.FailureAudit:
60- level = "W";
61- break;
62- case EventLogEntryType.Information:
63- case EventLogEntryType.SuccessAudit:
64- level = "I";
65- break;
66- default:
67- level = "I";
68- break;
69- }
70- if(e.Entry.TimeGenerated > DateTime.Now.AddDays(-1)){
71- FindAndExecute(new LogData {
72- Code = (int)(e.Entry.InstanceId & 0x3fffffff),
73- HostName = e.Entry.MachineName,
74- Source = e.Entry.Source,
75- Level = level,
76- TimeStamp = e.Entry.TimeGenerated,
77- Message = e.Entry.Message
78- });
79- }
8092 }
8193 }
8294 }
--- trunk/EWatch/EWatch/etc/EWatch.config.json (revision 89)
+++ trunk/EWatch/EWatch/etc/EWatch.config.json (revision 90)
@@ -134,7 +134,10 @@
134134 ],
135135 System: [
136136 { logLevel:'E', match:'term', ignore:null, actions:[ 'smtpTest' ]},
137- { logLevel:'E', match:null, ignore:null, actions:[ 'executeTest' ]},
137+ { logLevel:'E', match:null, ignore:null, actions:[ 'executeTest' ]}
138+ ],
139+ "Microsoft-Windows-TaskScheduler/Operational": [
140+ { logLevel:'E', match:null, ignore:null, actions:[ 'executeTest' ]}
138141 ]
139142 },
140143 File: {
--- trunk/EWatch/EWatchUI/FormRuleEdit.cs (revision 89)
+++ trunk/EWatch/EWatchUI/FormRuleEdit.cs (revision 90)
@@ -8,6 +8,8 @@
88 */
99 using System;
1010 using System.Collections.Generic;
11+using System.Diagnostics;
12+using System.Diagnostics.Eventing.Reader;
1113 using System.Drawing;
1214 using System.Linq;
1315 using System.Text;
@@ -97,8 +99,9 @@
9799 private void SetDataForEventLog()
98100 {
99101 comboBoxTarget.Items.Clear();
100- comboBoxTarget.Items.Add(EventLogTypeEnum.Application.ToString());
101- comboBoxTarget.Items.Add(EventLogTypeEnum.System.ToString());
102+ foreach(var logName in new EventLogSession().GetLogNames()){
103+ comboBoxTarget.Items.Add(logName);
104+ }
102105 buttonRef.Visible = false;
103106
104107 comboBoxLogLevel.Items.Clear();
--- trunk/EWatch/EWatchView/Receiver.cs (revision 89)
+++ trunk/EWatch/EWatchView/Receiver.cs (revision 90)
@@ -116,7 +116,7 @@
116116 }
117117 finally {
118118 if(tcpClient != null){
119- tcpClient.Dispose();
119+ tcpClient.Close();
120120 }
121121 }
122122 }
Show on old repository browser