c# 写入log 自己最常用的调试代码,可以适当 lock 【备用】非 log4
public static string WriteLogFileName(string strLog, string strFileName = "MyLog") { string strPath = System.Windows.Forms.Application.StartupPath + @"\Log\"; string strLogPathName = strPath + string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now).Substring(0, 9) + strFileName + ".log"; //lock (lockerMemoLog) { try { if (!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } string strLogMessage = string.Empty; StreamWriter swLog; { strLogMessage = string.Format("{0}:#{1}", DateTime.Now, (strLog)); } if (!File.Exists(strLogPathName)) { swLog = new StreamWriter(strLogPathName); } else { swLog = File.AppendText(strLogPathName); } swLog.WriteLine(strLogMessage); swLog.Close(); return "0"; } catch (Exception ex) { return ex.ToString(); } } }
编写了一个WinForm程序,项目文件存放于D:\Projects\Demo,编译后的文件位于D:\Projects\Demo\bin\Debug,最后的结果如下: 1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe 2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug 3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug 4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\ 5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\ 6、System.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug 7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\ System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\