개발 노트

로그파일저장 본문

프로그래밍/C#

로그파일저장

알 수 없는 사용자 2022. 3. 10. 10:33

MQTT 로그에서 TOPIC과 PAYLOAD를 저장

 

함수로 TOPIC인자와 PAYLOAD인자받기

	private void logSave2(string topic, string payload)
		{
			
			string currentPath = System.IO.Directory.GetCurrentDirectory();
			string[] lines = {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff - ") + topic + " - " + payload};

			using (StreamWriter Write = new StreamWriter(@"" + currentPath + "\\Log\\" + "log-MQTT-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",true))
			{
				foreach (string line in lines)
				{
					Write.WriteLine(line);
				}
			}

 

 

using System.IO;

 

string Path = @"경로\Write.txt";
string[] lines = {"첫번째 줄", "두번째 줄", "세번째줄"};

using (StreamWriter Write = new StreamWriter(path))
{
    foreach (string line in lines)
    {
       Write.WriteLine(line);
    }
}

 

using (StreamWriter Write = new StreamWriter(path, true))
{
    foreach (string line in lines)
    {
       Write.WriteLine(line);
    }
}

https://blog.naver.com/p_yubi/222147723542