![](/img/trans.png)
[英]Creating a new directory for log event file in windows service in c#
[英]Creating Log file for C# windows Application
我編寫了一個代碼塊來生成日志文件,以便可以調用所有功能。
我使用Microsoft.Practises.EnterpriseLibrary.Logging
dll獲取生成的日志文件,我知道如何獲取生成的日志文件。
string procName = "reports_plan_and_proj.plan_v_paid_inv_pay_method_sum";
Logger.Write("SQL:GetPlanvsPaidInvestPmtMthdSummary():: " + procName, "SQL");
讓我們像這樣使用一個類
public class LogControl
{
private static string _Path = string.Empty;
private static bool DEBUG = true;
public static void Write(string msg)
{
_Path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
try
{
using (StreamWriter w = File.AppendText(Path.Combine(_Path, "log.txt")))
{
Log(msg, w);
}
if (DEBUG)
Console.WriteLine(msg);
}
catch(Exception e)
{
//Handle
}
}
static private void Log(string msg, TextWriter w)
{
try
{
w.Write(Environment.NewLine);
w.Write("[{0} {1}]", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
w.Write("\t");
w.WriteLine(" {0}", msg);
w.WriteLine("-----------------------");
}
catch(Exception e)
{
//Handle
}
}
}
您使用LogControl.Write("I want to log this")
調用它,並產生如下輸出: [1:19:34 PM Friday, April 21, 2017] I want to log this -------------
: [1:19:34 PM Friday, April 21, 2017] I want to log this -------------
: [1:19:34 PM Friday, April 21, 2017] I want to log this -------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.