EaseFilter Demo Project
FileEventHandler.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2011 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.Collections.Generic;
13 using System.ComponentModel;
14 using System.Data;
15 using System.Runtime.InteropServices;
16 using System.Security.Principal;
17 using System.IO;
18 using System.Text;
19 using System.Threading;
20 using System.Reflection;
21 
23 
24 namespace FileMonitorService
25 {
26  //public enum FileEventType
27  //{
28  // NONE = 0,
29  // READ = 1,
30  // CREATE = 2,
31  // MODIFY = 4,
32  // DELETE = 8,
33  // RENAME = ,
34  // PERM
35  //}
36 
37  public enum FileEventResult
38  {
39  SUCCESS = 0,
40  FAILURE
41  }
42 
43  public class FileEvent : EventArgs
44  {
45  private FileAttributes fileAttributes = FileAttributes.Normal;
46  // type of the event -can be an enum
47  private FilterAPI.EVENTTYPE type = FilterAPI.EVENTTYPE.NONE;
48  // timestamp of event
49  private DateTime timestamp = DateTime.Now;
50  // full path of the resource (/ as path separator)
51  private string resource = string.Empty;
52  // user id
53  private string user = string.Empty;
54  // process name
55  private string process = string.Empty;
56  // result - can be an enum
57  private FileEventResult result = FileEventResult.SUCCESS;
58 
59  private string description = string.Empty;
60 
61  public FileEvent()
62  {
63  }
64 
65  public FileEvent(string _user,
66  string _process,
67  string _fileName,
68  FileAttributes _fileAttributes,
69  FilterAPI.EVENTTYPE _type,
70  DateTime _timestamp,
71  FileEventResult _result,
72  string _description)
73  {
74  this.user = _user;
75  this.process = _process;
76  this.resource = _fileName;
77  this.fileAttributes = _fileAttributes;
78  this.type = _type;
79  this.timestamp = _timestamp;
80  this.result = _result;
81  this.description = _description;
82  }
83 
87  public FileAttributes Attributes
88  {
89  get
90  {
91  return fileAttributes;
92  }
93 
94  set
95  {
96  fileAttributes = value;
97  }
98  }
99 
103  public FilterAPI.EVENTTYPE Type
104  {
105  get
106  {
107  return type;
108  }
109 
110  set
111  {
112  type = value;
113  }
114  }
115 
116 
120  public DateTime Timestamp
121  {
122  get
123  {
124  return timestamp;
125  }
126 
127  set
128  {
129  timestamp = value;
130  }
131  }
132 
136  public string Resource
137  {
138  get
139  {
140  return resource;
141  }
142 
143  set
144  {
145  resource = value;
146  }
147  }
148 
152  public string User
153  {
154  get
155  {
156  return user;
157  }
158 
159  set
160  {
161  user = value;
162  }
163  }
164 
168  public string Process
169  {
170  get
171  {
172  return process;
173  }
174 
175  set
176  {
177  process = value;
178  }
179  }
180 
184  public FileEventResult Result
185  {
186  get
187  {
188  return result;
189  }
190 
191  set
192  {
193  result = value;
194  }
195  }
196 
197 
201  public string Description
202  {
203  get
204  {
205  return description;
206  }
207 
208  set
209  {
210  description = value;
211  }
212  }
213  }
214 
215  public static class FileEventHandler
216  {
217 
218  static Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
219  static string assemblyPath = Path.GetDirectoryName(assembly.Location);
220 
221  static string filterMessageLogName = GlobalConfig.FilterMessageLogName;
222 
223  static string logFileName = Path.Combine(assemblyPath, filterMessageLogName);
224  static string logFolder = Path.GetDirectoryName(logFileName);
225 
226  static FileEventHandler()
227  {
228  if (!Directory.Exists(logFolder))
229  {
230  Directory.CreateDirectory(logFolder);
231  }
232 
233 
234  }
235 
236 
237  private static string GetLogMessage(FileEvent fileEvent)
238  {
239  string retVal = string.Empty;
240 
241  string fileType = "FILE";
242 
243  if ((fileEvent.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
244  {
245  fileType = "DIRECTORY";
246  }
247 
248  string eventType = string.Empty;
249  foreach (FilterAPI.EVENTTYPE type in Enum.GetValues(typeof(FilterAPI.EVENTTYPE)))
250  {
251  if ((fileEvent.Type & type) == type && type != FilterAPI.EVENTTYPE.NONE )
252  {
253  if (eventType.Length > 0)
254  {
255  eventType = eventType + ',' + type.ToString();
256  }
257  else
258  {
259  eventType = type.ToString();
260  }
261  }
262  }
263 
264  //the log message format
265  //File,Attributes,Time, Action, Success/Failure,Process, User, description
266 
267  retVal = fileEvent.Resource + "|" + fileType + "|" + fileEvent.Timestamp.ToString("yyyy-MM-ddTHH:mm:ss") + "|";
268  retVal += eventType + "|" + fileEvent.Result.ToString() + "|" + fileEvent.Process + "|";
269  retVal += fileEvent.User + "|" + fileEvent.Description;
270 
271  return retVal;
272 
273  }
274 
275  public static void LogFileEvent(FileEvent fileEvent)
276  {
277  try
278  {
279  string logMessage = GetLogMessage(fileEvent);
280 
281  if (logMessage.Trim().Length > 0)
282  {
283  if (File.Exists(logFileName))
284  {
285  FileInfo fileInfo = new FileInfo(logFileName);
286 
287  if (fileInfo.Length > GlobalConfig.FilterMessageLogFileSize)
288  {
289  File.Delete(logFileName + ".bak");
290  File.Move(logFileName, logFileName + ".bak");
291  }
292  }
293 
294  File.AppendAllText(logFileName, logMessage + "\r\n");
295  }
296  }
297  catch (Exception ex)
298  {
299  EventManager.WriteMessage(172, "LogTrasaction", EventLevel.Error, "Log filter message failed with error " + ex.Message);
300  }
301 
302  }
303 
304 
305  }
306 }
string Process
The process name
string Resource
Full path of the file name
FilterAPI.EVENTTYPE Type
type of the event -can be an enum
FileAttributes Attributes
the file attributes
DateTime Timestamp
// timestamp of event
FileEvent(string _user, string _process, string _fileName, FileAttributes _fileAttributes, FilterAPI.EVENTTYPE _type, DateTime _timestamp, FileEventResult _result, string _description)
FileEventResult Result
The status of the result
string Description
The description of the IO
ULONG eventType
Definition: FilterAPI.h:584

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com