我希望Windows服务中的日志文件应该以日期方式自动更新 [英] i want log file in windows service should be update automatically in date manner
问题描述
您好
使用C#在Windows服务中工作。我安装了这个服务,它运行良好,日志文件也运行良好。
我想以每日基本更新日志文件。任何人都可以帮助它..
这是我的代码..
公共静态类库
{
public static void writeErrorLog(string message)
{
StreamWriter sw = null;
尝试
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory +\\Logfile.txt,true);
sw.WriteLine(DateTime.Now.ToString()+:+ Message);
sw.Flush();
sw.Close();
}
catch
{
}
}
}
Hi
Am working in windows service using C#. I install the service and it working good and log file also working good.
I want to update the log file in daily basic. can anyone help it..
This is my code..
public static class Library
{
public static void writeErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + " : " + Message);
sw.Flush();
sw.Close();
}
catch
{
}
}
}
推荐答案
如果您想每天使用单独的日志文件,可以使用以下代码
you can use following code if you want separate log files for each day
string vrCurrentDate = DateTime.Now.ToString("ddMMyyyy");
FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\" + vrCurrentDate + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(DateTime.Now.ToString() + " : " + Message);
sw.Flush();
sw.Close();
希望这会对你有所帮助。如果没有创建,FileMode.OpenOrCreate将为每天创建新文件,否则它将更新当天的现有文件
hope this will help you. "FileMode.OpenOrCreate" will create new file for each day if not created already otherwise it will update that existing file for that day
这篇关于我希望Windows服务中的日志文件应该以日期方式自动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!