There are two tasks I am doing in my app. Each of this task can be imagined as a chain of method calls (some nested and some sequential). (Below are pseudocode just to clear context) Say Task1 can be imagined as:
method1(){
method2()//nested call in method1(){
method3();//nested call in method2() }}
method4()//seqiential to method1() and method5()
method5()//seqiential to method1() and method4()
Task2 can be imagined in similar way. The point is method nesting and sequence is dynamic based upon various ifs and elses.
Now my apps' main thread runs single thread of Task2 continuously, like:
while(true)Task2
And threads of Task1 are dispatched on demand based on the requests received on one of the HTTP port. So their will be multiple threads of Task1 running at a time, but single thread which runs Task2 continuously.
Now some methods in both tasks call some server APIs, I am measuring those turnaround time and logging in SQL Server. I wanted turnaround times of all server API calls made in different methods of a single thread go in a different cells of same record in log database.
To achieve this I coded logging functionality on my own. I created three classes:
Task1(){TurnaroundTime1::TurnaroundTime2
log(){//log TurnaroundTimeX properties to DB Table 1}}Task2(){TurnaroundTime1::TurnaroundTime2
log(){//log TurnaroundTimeX properties to DB Table 2}}LogManager{Dictionary<int,Task1> task1s =newDictionary<Task1>();Dictionary<int,Task2> task1s =newDictionary<Task2>();
createTask1(){
task1s[Thread.CurrentThread.ManagedThreadId]=Task1();}
createTask2(){
task2s[Thread.CurrentThread.ManagedThreadId]=Task2();}
removeTask1(){
task1s.Remove(Thread.CurrentThread.ManagedThreadId);}
removeTask2(){
task2s.Remove(Thread.CurrentThread.ManagedThreadId);}Task1CurrentTask1{
get{return task1s[Thread.CurrentThread.ManagedThreadId];}}Task2CurrentTask2{
get{return task2s[Thread.CurrentThread.ManagedThreadId];}}}//Now at the start task1 thread, I doLogManager.createTask1();//inside different methods in Task1 I set turnaround time propertiesLogManager.CurrentTask1.TurnaroundTime1= stopwatch.ElapsedMilliseconds;:://at the end of task1 thread, I doLogManager.CurrentTask1.log();LogManager.removeTask1();//similar I do for every iteration of Task2 thread
I want to ask
Q1. Am following wrong approach? Are their improvements?
Q2. If same logging approach can be immitated with NLog?