【发布时间】:2012-03-06 05:11:45
【问题描述】:
我们想添加一些“启动代码”来指示每次更新 ASP.NET 站点的二进制文件时的日志文件。因此,例如,我们有二进制版本 1.0,并将写入日志记录到 log_JhdsgGd1.txt。下次我们更正一些错误,然后上传新的二进制版本 1.1。现在,我们希望将日志文件写入其他一些 log_h12jh3G.txt。我们不会更改项目中的任何版本号。
我曾尝试在 Application_Start() 生成一些启动代码,然后在写入日志的类中使用它。但不幸的是,它并没有按预期工作。
发布更新
protected void Application_Start()
{
Random r = new Random();
Application["startCode"] = r.Next(100000).ToString();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
public static class Logger
{
static internal void WriteLog(string msg, string dbname = null)
{
using (StreamWriter sw = new StreamWriter(String.Format("{0}{1}{2}", HttpContext.Current.Server.MapPath("log"), Path.DirectorySeparatorChar, String.Format("log_{0}.txt", HttpContext.Current.Application["startCode"]), true)))
{
string logEntry;
if (!String.IsNullOrEmpty(dbname))
logEntry = String.Format("{0} ({1}): {2}", DateTime.Now.ToString(), dbname, msg);
else
logEntry = String.Format("{0}: {1}", DateTime.Now.ToString(), msg);
sw.WriteLine(logEntry);
sw.WriteLine("------------");
sw.Flush();
sw.Close();
}
}
}
【问题讨论】:
-
能不能贴出不起作用的代码?
-
请查看更新后的问题。
标签: c# asp.net .net asp.net-mvc-2 logging