
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 1.82 KB | hits: 20 | expires: Never
better way of invoking thread and logging to text file in C#
namespace logging
{
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
public class log
{
private static string lpath;
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public static void Info(string user, string info, string txt)
{
StreamWriter writer;
string str = Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();
.....
if (!File.Exists(path))
{
writer = new StreamWriter(path, true);
writer.WriteLine(str3 + " " + info + " => " + txt);
writer.Flush();
writer.Close();
}
else
{
writer = File.AppendText(path);
writer.Write(str3 + " " + info + " => " + txt + "n");
writer.Flush();
writer.Close();
}
}
}
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Thread thread_BP_Print = new Thread(BoardingPass_Print);
thread_BP_Print.Start();
// Simultaneously, do something on the main thread.
BaggageTag_Print();
bgw.RunWorkerAsync();
}
logging.log.Info(username, "Query Trace > ", sql);
private static object locker = new object();
public static void Info(string user, string info, string txt)
{
string str = Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();
string str3 = ...
lock (locker)
{
File.AppendAllText(path, str3 + " " + info + " => " + txt + "n");
}
}