- เพิ่ม Reference ชื่อ System.Configuration
- กำหนด config ในไฟล์ App.config
- สร้างคลาส LogClass
- Program.cs
1.เพิ่ม Reference ชื่อ System.Configuration
ที่ Solution Explorer
ใต้โปรเจ็กส์ ให้คลิกขวาที่ References
เลือก Add Reference…

จะเห็นไดอะล็อก Reference Manager
เลือก Assemblies
พิมพ์ค้นหาด้วยคำว่า configuration
เลือก System.Configuration แล้วคลิก OK

2.กำหนด config ในไฟล์ App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="LogPath" value="\log\" />
<add key="LogName" value="TX" />
<add key="Debug" value="true" />
<add key="Sensible" value="3" />
<add key="BoxWidth" value="25" />
</appSettings>
</configuration>
- บรรทัดที่ 8: กำหนด path ที่ต้องการเก็บไฟล์ log
- บรรทัดที่ 9: กำหนดคำต่อท้ายชื่อไฟล์
- บรรทัดที่ 10: ถ้า Debug เป็น flase จะไม่ทำการเขียน log
- บรรทัดที่ 11: ต้องลองรัน แล้วเปลี่ยนค่าดู
- บรรทัดที่ 12: ความกว้างของกล่องที่ใช้แสดงชื่อฟังก์ชัน
3.สร้างคลาส LogClass
สร้างโฟลเดอร์ mylib ไว้เก็บคลาส จากนั้นสร้างไฟล์ mylib/LogClass.cs
mylib/LogClass.cs
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
namespace ConsoleApp1.mylib
{
class LogClass
{
public static void Log(string message)
{
if (!Convert.ToBoolean(ConfigurationManager.AppSettings.Get("Debug")))
return;
// path
string logPath = AppDomain.CurrentDomain.BaseDirectory
+ ConfigurationManager.AppSettings.Get("LogPath");
// create if it is not exist
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
// filename
string filename = logPath
+ DateTime.Today.ToString("yyyyMMdd", new CultureInfo("en-US"))
+ "_"
+ ConfigurationManager.AppSettings.Get("LogName")
+ ".log";
int someSensible = Convert.ToInt32(ConfigurationManager.AppSettings.Get("Sensible"));
int count = Environment.StackTrace.Count(a => a == '\n') - someSensible;
string indent = new string(' ', Math.Max(0, count) * 4);
string st = string.Format("[{0}] {1}{2}", DateTime.Now.ToString("HH:mm:ss", new CultureInfo("en-US")), indent, message);
using (StreamWriter w = File.AppendText(filename))
{
Console.WriteLine(st);
w.WriteLine(st);
}
}
}
}
4.Program.cs
Program.cs
using ConsoleApp1.mylib;
using System;
using System.Configuration;
using System.Reflection;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int width = Convert.ToInt32(ConfigurationManager.AppSettings["BoxWidth"]);
int space = width - MethodBase.GetCurrentMethod().Name.Length - 5;
LogClass.Log(string.Format("{0}", new string('-', width)));
LogClass.Log(string.Format("| {0} {1} |", MethodBase.GetCurrentMethod().Name, new string(' ', space)));
LogClass.Log(string.Format("{0}", new string('-', width)));
FirstMethod();
}
static void FirstMethod()
{
LogClass.Log(MethodBase.GetCurrentMethod().Name);
SecondMethod();
}
static void SecondMethod()
{
LogClass.Log(MethodBase.GetCurrentMethod().Name);
ThirdMethod();
}
static void ThirdMethod()
{
LogClass.Log(MethodBase.GetCurrentMethod().Name);
}
}
}
ตัวอย่างผลลัพธ์ไฟล์ log/20190211_TX.log
[22:11:58] ------------------------- [22:11:58] | Main | [22:11:58] ------------------------- [22:11:58] FirstMethod [22:11:58] SecondMethod [22:11:58] ThirdMethod