성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
[정성태] 만드실 수 있습니다. 단지, Unity 엔진 내의 스크립트와 W...
[공진영] 안녕하세요 좋은글 감사합니다. 현재 제가 wpf로 관제 모...
[정성태] The Windows Registry Adventure #1: ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - ETW EventListener를 상속받았을 때 초기화 순서</h1> <p> 지난 글에서 실습한 CLR ETW 예제를,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - (.NET Core 2.2부터 가능한) 프로세스 내부에서 CLR ETW 이벤트 수신 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12474'>https://www.sysnet.pe.kr/2/0/12474</a> </pre> <br /> 약간 변경해 볼까요? ^^<br /> <br /> 예를 들어, "Microsoft-Windows-DotNETRuntime"의 특정 Keyword 영역에 해당하는,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > CLR ETW Keywords and Levels ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/framework/performance/clr-etw-keywords-and-levels#clr-etw-runtime-keywords'>https://learn.microsoft.com/en-us/dotnet/framework/performance/clr-etw-keywords-and-levels#clr-etw-runtime-keywords</a> </pre> <br /> 이벤트만 처리하도록 생성자 측에 관심 있는 Keyword 영역을 지정하는 식으로 코딩을 해보겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Diagnostics.Tracing; class Program { static MyEventListener _listener; static Program() { _listener = <span style='color: blue; font-weight: bold'>new MyEventListener(DotnetRuntimeKeyword.EXCEPTIONKEYWORD);</span> } // ...[생략]... } internal class MyEventListener : EventListener { int _filterKeyword; <span style='color: blue; font-weight: bold'>public MyEventListener(DotnetRuntimeKeyword filterKeyword) { this._filterKeyword = (int)filterKeyword; }</span> <span style='color: blue; font-weight: bold'>protected override void OnEventSourceCreated(EventSource eventSource)</span> { if (eventSource.Name == "Microsoft-Windows-DotNETRuntime") { <span style='color: blue; font-weight: bold'>EnableEvents</span>(eventSource, EventLevel.Informational, <span style='color: blue; font-weight: bold'>(EventKeywords)this._filterKeyword</span>); return; } } protected override void OnEventWritten(EventWrittenEventArgs eventData) { Console.WriteLine($"{DateTime.Now}: {eventData.EventName}"); } } // ...[생략]... </pre> <br /> 위의 코드를 직접 실행해 보면, <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.tracing.eventlistener.oneventwritten'>OnEventWritten 콜백</a>이 단 한 번도 발생하지 않게 됩니다. 왜냐하면 OnEventSourceCreated 단계에서의 EnableEvent에 전달한 this._filterKeyword의 값이 0이기 때문입니다. 아니, 분명히 생성자를 통해 filterKeyword 값을 전달했는데 왜 0이 나오는 걸까요?<br /> <br /> 그 이유는 MyEventListener 생성자 단계에서 부모 클래스(<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.tracing.eventlistener'>EventListener</a>)의 생성자를 호출하게 되고, OnEventSourceCreated 콜백은 바로 그 EventListener의 생성자 단계에서 발생하기 때문입니다. 이로 인해 결국 MyEventListener의 OnEventSourceCreated 콜백에서는 _filterKeyword 필드가 아직 초기화되지 않아 그와 같은 현상이 나오는 것입니다.<br /> <br /> 이 문제를 우회하려면 간단하게 static 필드를 사용할 수 있겠으나,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Diagnostics.Tracing; class Program { static MyEventListener _listener; static Program() { <span style='color: blue; font-weight: bold'>MyEventListener._filterKeyword = (int)DotnetRuntimeKeyword.EXCEPTIONKEYWORD;</span> _listener = new MyEventListener(); } // ...[생략]... } internal class MyEventListener : EventListener { <span style='color: blue; font-weight: bold'>public static int _filterKeyword</span>; protected override void OnEventSourceCreated(EventSource eventSource) { if (eventSource.Name == "Microsoft-Windows-DotNETRuntime") { EnableEvents(eventSource, EventLevel.Informational, (EventKeywords)<span style='color: blue; font-weight: bold'>MyEventListener._filterKeyword</span>); return; } } } </pre> <br /> 보다시피 별로 좋은 해결책은 아닙니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 의도한 것인지는 알 수 없으나, EventListener는 이에 대해 별도의 이벤트를 제공하는 것으로 해결할 수 있습니다. 즉, override로 제공하는 OnEventSourceCreated를 사용하는 대신 다음과 같이 이벤트를 구독하는 방식으로 구현하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... internal class MyEventListener : EventListener { public int _filterKeyword; public MyEventListener(DotnetRuntimeKeyword filterKeyword) { this._filterKeyword = (int)filterKeyword; <span style='color: blue; font-weight: bold'>this.EventSourceCreated += MyEventListener_EventSourceCreated;</span> } private void <span style='color: blue; font-weight: bold'>MyEventListener_EventSourceCreated</span>(object sender, EventSourceCreatedEventArgs e) { if (e.EventSource.Name == "Microsoft-Windows-DotNETRuntime") { EnableEvents(e.EventSource, EventLevel.Informational, (EventKeywords)this._filterKeyword); } } } </pre> <br /> MyEventListener_EventSourceCreated 이벤트 처리기는 MyEventListener의 초기화가 완료된 이후에 호출되는 식으로 처리가 됩니다. 그래서 생성자에 전달된 값을 기반으로 한 이후의 대응이 가능한데... 어쩐지 이 대목에서 "<a target='tab' href='https://caseymuratori.com/blog_0025'>THE WORST API EVER MADE</a>" 글이 다시 한번 생각나는 순간입니다. ^^;<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1755&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1633
(왼쪽의 숫자를 입력해야 합니다.)