Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log file not created inside Task.Factory.StartNew()

For the following sample code, I don't see the Log file "ParallelLog.txt" created in C: drive. (Apologies for the big chunk of code!)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;

namespace Logger
{
    internal class Program
    {
        static List<Employee> employees = new List<Employee>()
        {
            new Employee{Name = "William", Age=30, RollNo=1234},
            new Employee{Name = "Martin", Age=29, RollNo=1235},
            new Employee{Name = "Richard", Age=28, RollNo=1236},
            new Employee{Name = "Baldwin", Age=27, RollNo=1237}
        };

        static void Main(string[] args)
        {
            Task task = Task.Factory.StartNew(() =>
            {
                foreach (Employee employee in employees.AsParallel().AsOrdered())
                {
                    WriteToLog(employee);
                }
            });
        }

        public static void WriteToLog(Employee employee)
        {            
            static string fileName = @"C:\ParallelLog.txt";
            private static object lockObject = new object();

            lock (lockObject)
            {
                StreamWriter writer = new StreamWriter(fileName, true);
                writer.WriteLine("{0} - {1} - {2}", employee.Name, employee.Age, employee.RollNo);
                writer.Close();
                writer.Dispose();
                writer = null;
            }
        }
    }

    internal class Employee
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int RollNo { get; set; }
    } 
}

Is there something wrong in my code above? This sample is the base for my actual project's log file creation process.

like image 639
Sriram B Avatar asked Feb 15 '26 07:02

Sriram B


1 Answers

Try the code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;

namespace Logger
{
    internal class Program
    {
        static List<Employee> employees = new List<Employee>()
        {
            new Employee{Name = "William", Age=30, RollNo=1234},
            new Employee{Name = "Martin", Age=29, RollNo=1235},
            new Employee{Name = "Richard", Age=28, RollNo=1236},
            new Employee{Name = "Baldwin", Age=27, RollNo=1237}
        };

        static void Main(string[] args)
        {
            Task task = Task.Factory.StartNew(() =>
            {
                WriteToLog();
            });
        }

        public static void WriteToLog()
        {            
            static string fileName = @"C:\ParallelLog.txt";
            using (StreamWriter writer = new StreamWriter(fileName, true))
            {
                foreach (Employee employee in employees.AsParallel().AsOrdered())
                    writer.WriteLine("{0} - {1} - {2}", employee.Name, employee.Age, employee.RollNo);
            }
        }
    }

    internal class Employee
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int RollNo { get; set; }
    } 
}

In this case there is no need for the use of a lock. This is removing the successive instantiation of a StreamWriter object to just one. Here you do not have to worry about management as this is handled by the using keyword. Please see MSDN for recommended way to use the StreamWriter class.

I hope this helps.

like image 88
MoonKnight Avatar answered Feb 16 '26 20:02

MoonKnight