【问题标题】:New log file every time we uploaded new binaries每次我们上传新的二进制文件时都有新的日志文件
【发布时间】: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


【解决方案1】:

您能否将 SVN/GIT 修订版链接到您的项目?或者计算你的二进制文件的MD5

【讨论】:

  • 从二进制计算 MD5 看起来很有趣。
  • 你能提供一些样本吗?
  • 我认为你不需要特别的东西,所以简单的 MD5 哈希就可以了:string filename = "myfile.exe"; MD5 md5 = MD5CryptoServiceProvider.Create(); string md5 = Common.EncodeHex(md5.ComputeHash(File.OpenRead(filename)));Common.EncodeHex() 是编码 byte[] ar 字符串的方法,你可以使用你自己的
  • 最好不要 :) 在这种情况下,您将不知道文件何时更改
  • 在Application_Start()中添加md5代码计算怎么样?
猜你喜欢
  • 2014-12-24
  • 1970-01-01
  • 2015-03-21
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
相关资源
最近更新 更多