1. Console.WriteLine()
WP7 개발하면서 처음으로 난감했던게..
Console.WriteLine()을 해도 출력창(Ctrl+W, O)에서 보이지 않는 문제였다.
현재 실제 단말이 없어서 에뮬상에서 테스트를 하고 있는데,
자세히 살펴보면.. 에뮬 실행시 검은색 Console 창이 뜨고 바로 사라지면서 폰모양의 에뮬이 뜨는 것을 알 수 있다.
따라서 Console 창이 사라지지 않게 하면 되는데..
레지스트리를 수정하면 콘솔창이 사라지지 않게하여 Console.WriteLine()으로 출력한 내용을 볼 수 있다.
근데.. 한글은 제대로 보이지 않는다.
32-bit OS에서는
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\XDE]
64-bit OS에서는
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\XDE]
DWORD값으로 EnableConsole=1 을 추가
참고 : http://forums.create.msdn.com/forums/p/60378/372138.aspx#372138
2. Debug.WriteLine()
System.Diagnostics.Debug.WriteLine()을 사용하면
Visual Sudio의 출력창에서 정상적으로 로그를 확인할 수 있다.
사용해보지는 않았지만, 실제 단말에서도 잘 작동한다고 한다.
참고로 에뮬의 Console창에서도 확인할 수 있지만 한글은 깨진다.
3. NLog 사용
사용법
오픈소스로 되어 있는 NLog는 .NET, Silverlight, Windows Phone을 지원하며,
사용법은 Apache의 Log4Net과 거의 유사하다.
NLog는 아래와 같은 Target으로 로그를 출력할 수 있다고 한다.
- Files : single file or multiple, with automatic file naming and archival
- Event Log : local or remote
- Database : store your logs in databases supported by .NET
- Network : using TCP, UDP, SOAP, MSMQ protocols
- Command-line console : including color coding of messages
- E-mail : you can receive emails whenever application errors occur
- ASP.NET trace
- … and many more
기본적인 NLog 사용법은 아래 URL을 참고한다.
http://nlog-project.org/wiki/Tutorial
윈폰에서 Consumer용 App이라면 log가 별로 불필요할테지만,
Enterprise용 App이라면 log를 원격으로 생성하여 관리할 필요도 있을듯하다.
이럴 경우 아래를 참고하면 뚝딱 만들 수 있다.
http://nlog-project.org/2011/01
Windows Phone 쪽 로그출력
private void button1_Click(object sender, RoutedEventArgs e) { switch (listPicker1.SelectedItem.ToString()) { case "Trace": m_logger.Trace(textBox1.Text); break; case "Debug": m_logger.Debug(textBox1.Text); break; case "Info": m_logger.Info(textBox1.Text); break; case "Warn": m_logger.Warn(textBox1.Text); break; case "Error": m_logger.Error(textBox1.Text); break; case "Fatal": m_logger.Fatal(textBox1.Text); break; } //Console.WriteLine("Console.WriteLine : " + textBox1.Text); //System.Diagnostics.Debug.WriteLine("Debug.WriteLine : " + textBox1.Text); }
Windows Phone 쪽 로그설정 (NLog.config)
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target xsi:type="LogReceiverService" name="webService" endpointAddress="http://localhost:5000/LogReceiver.svc"> <parameter name="t" layout="${threadid}" /> </target> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="webService" /> </rules> </nlog>
수신측 코드
namespace NLogReceiverForwarderService { using System; using System.ServiceModel; using NLog.LogReceiverService; class Program { static void Main(string[] args) { try { var uri = new Uri("http://localhost:5000/LogReceiver.svc"); var host = new ServiceHost(typeof(LogReceiverForwardingService), uri); var binding = new BasicHttpBinding(); host.AddServiceEndpoint(typeof(ILogReceiverServer), binding, uri); host.Open(); Console.WriteLine("Host opened."); Console.ReadLine(); host.Close(); Console.WriteLine("Host closed."); } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex.ToString()); Console.ReadLine(); } } } }
수신측 로그설정 (NLog.config)
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target name="console" xsi:type="Console" layout="${longdate} ${message} ${event-context:t}" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="console" /> </rules> </nlog>
Notice
