统计windows服务c#执行的任务类型
Count the type of tasks executed by a windows service c#
我有一个使用 c# 开发的 windows 服务。该服务一直在查看特定的文件夹路径,比如 inputfolder
,如果添加了任何新的文本文件,它会选择该文件,对其进行处理并生成输出并将其写入另一个日志文件并从中删除该文件inputfolder
.
现在 inputfolder
中的数据可以是不同的类型,例如 TypeA
和 TypeB
。这个类型在读取文本文件中的数据后就知道了。每个文件都有 TypeA
或 TypeB
而不是 both.I 能够读取 txt 文件并获取文件类型。
我这里的要求是在一天结束的时候,我应该能够分辨出有多少TypeA
个任务有运行和有多少TypeB
个任务有run.So 为此,我正在考虑执行以下操作之一:
声明一个静态变量,在应用程序中说 countA
和 countB
,一旦我得到类型,就增加适当的变量。但如果由于某种原因,服务必须在一天之间停止和启动,我将丢失数据。
将这些详细信息写入单独的文件,例如 txt 或日志文件。首先读取文件,获取现有计数,增加它并再次覆盖新值。
任何人都可以建议,这是执行此操作的最佳方法。如果有人觉得上面的方法很粗糙,请guide/suggest告诉我更好的方法。
非常感谢!
或者
- 存储在内存中,数据变化时持久化到文件或注册表。
- 或者将其存储在数据库中,同样,或者创建一个带有任务和开始日期的 table。您可以通过各种方式查询
当服务加载读取该数据时,将其存储在内存中。
任务完成
正如您所说,这取决于您的要求。如果计数器值很重要,那么服务中的任何错误都可能会丢失主题,您应该在本地存储最新的计数器值。
对于如何以及在何处存储计数器值的场景,有多种选择:
- 将计数器存储在磁盘文件中并保持文件最新
- 将计数器存储在注册表项中
你的代码应该是这样的:
public static void Main()
{
var result = IncCounter(@"D:.txt", FileType.TypeA);
Console.WriteLine($"TypeA : {result.FileTypeACounter} - TypeB : {result.FileTypeBCounter}");
Console.Read();
}
public enum FileType
{
TypeA,
TypeB
}
[Serializable]
public class FileTypeCounter
{
public int FileTypeACounter { get; set; }
public int FileTypeBCounter { get; set; }
}
public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
{
using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, objectToWrite);
}
}
public static T ReadFromBinaryFile<T>(string filePath)
{
using (Stream stream = File.Open(filePath, FileMode.Open))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
return (T)binaryFormatter.Deserialize(stream);
}
}
public static FileTypeCounter IncCounter(string counterFilePath, FileType fileType)
{
try
{
var fileTypeCounter = new FileTypeCounter { FileTypeACounter = 0, FileTypeBCounter = 0 };
if (!File.Exists(counterFilePath))
{
if (fileType == FileType.TypeA)
fileTypeCounter.FileTypeACounter++;
else
fileTypeCounter.FileTypeBCounter++;
WriteToBinaryFile<FileTypeCounter>(counterFilePath, fileTypeCounter);
return fileTypeCounter;
}
fileTypeCounter = ReadFromBinaryFile<FileTypeCounter>(counterFilePath);
switch (fileType)
{
case FileType.TypeA:
fileTypeCounter.FileTypeACounter++;
break;
case FileType.TypeB:
fileTypeCounter.FileTypeBCounter++;
break;
}
WriteToBinaryFile<FileTypeCounter>(counterFilePath, fileTypeCounter);
return fileTypeCounter;
}
catch (Exception ex)
{
return null;
}
}
我有一个使用 c# 开发的 windows 服务。该服务一直在查看特定的文件夹路径,比如 inputfolder
,如果添加了任何新的文本文件,它会选择该文件,对其进行处理并生成输出并将其写入另一个日志文件并从中删除该文件inputfolder
.
现在 inputfolder
中的数据可以是不同的类型,例如 TypeA
和 TypeB
。这个类型在读取文本文件中的数据后就知道了。每个文件都有 TypeA
或 TypeB
而不是 both.I 能够读取 txt 文件并获取文件类型。
我这里的要求是在一天结束的时候,我应该能够分辨出有多少TypeA
个任务有运行和有多少TypeB
个任务有run.So 为此,我正在考虑执行以下操作之一:
声明一个静态变量,在应用程序中说
countA
和countB
,一旦我得到类型,就增加适当的变量。但如果由于某种原因,服务必须在一天之间停止和启动,我将丢失数据。将这些详细信息写入单独的文件,例如 txt 或日志文件。首先读取文件,获取现有计数,增加它并再次覆盖新值。
任何人都可以建议,这是执行此操作的最佳方法。如果有人觉得上面的方法很粗糙,请guide/suggest告诉我更好的方法。
非常感谢!
或者
- 存储在内存中,数据变化时持久化到文件或注册表。
- 或者将其存储在数据库中,同样,或者创建一个带有任务和开始日期的 table。您可以通过各种方式查询
当服务加载读取该数据时,将其存储在内存中。
任务完成
正如您所说,这取决于您的要求。如果计数器值很重要,那么服务中的任何错误都可能会丢失主题,您应该在本地存储最新的计数器值。
对于如何以及在何处存储计数器值的场景,有多种选择:
- 将计数器存储在磁盘文件中并保持文件最新
- 将计数器存储在注册表项中
你的代码应该是这样的:
public static void Main()
{
var result = IncCounter(@"D:.txt", FileType.TypeA);
Console.WriteLine($"TypeA : {result.FileTypeACounter} - TypeB : {result.FileTypeBCounter}");
Console.Read();
}
public enum FileType
{
TypeA,
TypeB
}
[Serializable]
public class FileTypeCounter
{
public int FileTypeACounter { get; set; }
public int FileTypeBCounter { get; set; }
}
public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
{
using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, objectToWrite);
}
}
public static T ReadFromBinaryFile<T>(string filePath)
{
using (Stream stream = File.Open(filePath, FileMode.Open))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
return (T)binaryFormatter.Deserialize(stream);
}
}
public static FileTypeCounter IncCounter(string counterFilePath, FileType fileType)
{
try
{
var fileTypeCounter = new FileTypeCounter { FileTypeACounter = 0, FileTypeBCounter = 0 };
if (!File.Exists(counterFilePath))
{
if (fileType == FileType.TypeA)
fileTypeCounter.FileTypeACounter++;
else
fileTypeCounter.FileTypeBCounter++;
WriteToBinaryFile<FileTypeCounter>(counterFilePath, fileTypeCounter);
return fileTypeCounter;
}
fileTypeCounter = ReadFromBinaryFile<FileTypeCounter>(counterFilePath);
switch (fileType)
{
case FileType.TypeA:
fileTypeCounter.FileTypeACounter++;
break;
case FileType.TypeB:
fileTypeCounter.FileTypeBCounter++;
break;
}
WriteToBinaryFile<FileTypeCounter>(counterFilePath, fileTypeCounter);
return fileTypeCounter;
}
catch (Exception ex)
{
return null;
}
}