Count the type of tasks executed by a windows service c#

Count the type of tasks executed by a windows service c#



I have a windows service developed using c#. This service keeps looking into a particular folder path say inputfolder, if there is any new text file added, it picks up the file, process it and generates an output and writes it into another log file and deletes the file from the inputfolder.


inputfolder


inputfolder



Now the data in inputfolder can be of different types, say TypeA and TypeB for example. This type is known after reading the data in the text file. Each file has either TypeA or TypeB and not both.I'm able to read the txt file and get the type of file.


inputfolder


TypeA


TypeB


TypeA


TypeB



My requirement here is at the end of the day, I should be able to tell how many TypeA tasks have run and how many TypeB tasks have run.So for this I'm thinking of doing one of the following:


TypeA


TypeB



Declare a static variables ,say countA and countB in the application and as soon as I get the type, increment the appropriate variable. But if for some reason , the service has to be stopped and started in between the day, I will loose the data.


countA


countB



Write these details into a separate file say txt or log file. Read the file first, get the existing count, increment it and overwrite the new value again.



Can any one suggest , which is the best way to do this. If anyone feels that the above method is very crude way of doing, kindly guide/suggest me the better way of doing it.



Many thanks in advance!






Please do not down vote if the question is wrong, instead help me correct it.Thanks!

– CrazyCoder
Sep 11 '18 at 11:05




2 Answers
2



As you say it depends of your requirements. if the counter value is important it's really possible to lose theme by any fault in service and you should store latest counter value locally.



And for the scenario of how and where to store counter value there is multiple choices :



You code should be something like this :


public static void Main()

var result = IncCounter(@"D:1.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;







Thanks for the reply. Your example above is very helpful. But , since I have to store 2 counter values, how do I differentiate between those 2 types. Say suppose the file has Count of TypeA : 13 (next line) Count of TypeB : 15 , how do I read it differently?

– CrazyCoder
Sep 11 '18 at 14:03






I change the code to serialize entire class into a binary file and vice versa. you can add more additional fields to "FileTypeCounter" class without need to change the code.

– Mojtaba Tajik
Sep 11 '18 at 14:32






I have tried the above code. It is writing ÿÿÿÿ in the text file.

– CrazyCoder
Sep 11 '18 at 16:14






That's normal behavior.The entire class got serialized and stored in file, you can't read the content by text editor but the code cloud deserialize the file and read the content. i change the code again to view the result, check it and read the code carefully to understand what happen.

– Mojtaba Tajik
Sep 11 '18 at 17:53



Either



When the service loads read that data, store it in memory.



Job done






Thanks for the reply. The task has to be done without using the database. Can you explain the first point, what do you mean by store in memory? You mean write on a file?

– CrazyCoder
Sep 11 '18 at 14:00



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)