성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
글쓰기
제목
이름
암호
전자우편
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 이벤트의 Keywords에 속한 EventId 구하는 방법 (2) 관리 코드</h1> <p> 지난 글에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - ETW 이벤트의 Keywords에 속한 EventId 구하는 방법 (1) PInvoke ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12612'>https://www.sysnet.pe.kr/2/0/12612</a> </pre> <br /> PInvoke를 활용해 ETW의 Keyword에 속한 이벤트 목록을 구했는데요, 이게 좀 아쉬운 면이 있다면 사실 "<a target='tab' href='https://www.sysnet.pe.kr/2/0/12474'>C# - (.NET Core 2.2부터 가능한) 프로세스 내부에서 CLR ETW 이벤트 수신</a>"은 제목에서처럼 .NET Framework 대상이 아닌 .NET Core 2.2부터 가능한 기능입니다. 그런 면에서 리눅스를 대상으로 작성될 가능성도 적지 않은데 그런 상황에서 PInvoke를 사용하는 것은 바람직한 선택은 아닙니다.<br /> <br /> 여기서 더욱 아쉬운 면이 있다면, (지난 글에도 언급했지만) 순수하게 C# 코드만으로는 공식적으로 Event ID를 구할 수 있는 방법이 없다는 것인데요, 그래서 안정적인 코드를 사용해야 한다면 미리 이벤트 ID 목록을 알아내 하드 코딩하는 것이 현재로서는 최선입니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그나마 다행이라면, "비공식적인 방법"으로는 Event ID 목록을 구할 수는 있습니다.<br /> <br /> 왜냐하면, <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.tracing.eventsource'>EventSource 타입</a>은 내부에 private 필드로는 <a target='tab' href='CLR-ETW.man'>CLR-ETW.man</a>에 지정된 이벤트 목록을 System.Diagnostics.Tracing.EventSource.EventMetadata 타입의 배열로 들고 있기 때문입니다. 그래서 다음과 같은 절차의 Reflection 연산을 통해 이 목록을 구할 수 있습니다. ^^<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 : <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.tracing.eventlistener'>EventListener</a> { // <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/framework/performance/clr-etw-keywords-and-levels'>https://docs.microsoft.com/en-us/dotnet/framework/performance/clr-etw-keywords-and-levels</a> public const int GCKEYWORD = 0x00000001; public const int EXCEPTIONKEYWORD = 0x00008000; protected override void OnEventSourceCreated(EventSource eventSource) { if (eventSource.Name == "Microsoft-Windows-DotNETRuntime") { base.OnEventSourceCreated(eventSource); EnableEvents(eventSource, EventLevel.Informational, (EventKeywords)EXCEPTIONKEYWORD); <span style='color: blue; font-weight: bold'>ListEventID(eventSource, EXCEPTIONKEYWORD);</span> return; } } private void ListEventID(EventSource eventSource, int filterKeyword) { Type type = eventSource.GetType(); FieldInfo fi = type.GetField(<span style='color: blue; font-weight: bold'>"m_eventData"</span>, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (fi != null) { <span style='color: blue; font-weight: bold'>object objMetadataArray = fi.GetValue(eventSource); // System.Diagnostics.Tracing.EventSource.EventMetadata []</span> System.Collections.IEnumerable enumerator = objMetadataArray as System.Collections.IEnumerable; foreach (object objItem in enumerator) { <span style='color: blue; font-weight: bold'>object descriptor = GetDescriptor(objItem);</span> if (descriptor == null) { continue; } <span style='color: blue; font-weight: bold'>(long keyword, int eventId) = GetKeywordAndEventId(descriptor); if (((int)keyword & filterKeyword) == filterKeyword) { Console.WriteLine($"{filterKeyword} == {eventId}"); }</span> } } } private (long, int) GetKeywordAndEventId(object descriptor, out int eventId) { object keyword = GetPropertyReflect(descriptor, "Keywords"); object eventValue = GetPropertyReflect(descriptor, "EventId"); if (keyword == null || eventValue == null) { return (0, 0); } return ((long)keyword, (int)eventValue); } private object GetDescriptor(object objItem) { return GetFieldReflect(objItem, "Descriptor"); } object GetPropertyReflect(object obj, string propertyName) { Type type = obj.GetType(); PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); if (pi != null) { return pi.GetValue(obj); } return null; } object GetFieldReflect(object obj, string fieldName) { Type type = obj.GetType(); FieldInfo fi = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance); if (fi != null) { return fi.GetValue(obj); } return null; } protected override void OnEventWritten(EventWrittenEventArgs eventData) { Console.WriteLine($"{DateTime.Now}: {eventData.EventName}"); } } /* 출력 결과 32768 == 80 32768 == 250 32768 == 251 32768 == 252 32768 == 253 32768 == 254 32768 == 255 32768 == 256 */ </pre> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1752&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
3021
(왼쪽의 숫자를 입력해야 합니다.)