Visual Basic .NET에서 추적 및 디버그 클래스 사용
이 문서에서는 Visual Basic .NET에서 및 클래스를 Trace
사용하는 Debug
방법에 대한 정보를 제공합니다.
원래 제품 버전: Visual Basic .NET
원본 KB 번호: 313417
요약
이 문서에서는 및 Trace
클래스를 Debug
사용하는 방법을 보여 줍니다. 이러한 클래스는 Microsoft .NET Framework 사용할 수 있습니다. 이러한 클래스를 사용하여 애플리케이션 개발 중 또는 프로덕션에 배포한 후 애플리케이션의 성능에 대한 정보를 제공할 수 있습니다. 이러한 클래스는 .NET Framework 사용할 수 있는 계측 기능의 한 부분일 뿐입니다.
요구 사항
다음 목록에서는 필요한 권장 하드웨어, 소프트웨어, 네트워크 인프라 및 서비스 팩을 간략하게 설명합니다.
- Windows
- Visual Basic .NET
또한 이 문서에서는 프로그램 디버깅에 익숙하다고 가정합니다.
기술에 대한 설명
디버그 클래스를 사용하여 샘플 만들기 섹션의 단계에서는 클래스를 사용하여 Debug
프로그램 실행에 대한 정보를 제공하는 콘솔 애플리케이션을 만드는 방법을 보여 줍니다.
프로그램이 실행되면 클래스의 메서드를 Debug
사용하여 모니터링하거나, 오작동을 감지하거나, 성능 측정 정보를 제공하는 데 도움이 되는 메시지를 생성할 수 있습니다. 기본적으로 클래스에서 Debug
생성하는 메시지는 Microsoft Visual Studio IDE(통합 개발 환경)의 출력 창에 표시됩니다.
샘플 코드는 메서드를 WriteLine
사용하여 줄 종결자 뒤에 메시지를 생성합니다. 이 메서드를 사용하여 메시지를 생성하면 각 메시지가 출력 창의 별도 줄에 표시됩니다.
클래스의 메서드를 Assert
Debug
사용하는 경우 지정된 조건이 false로 평가되는 경우에만 출력 창에 메시지가 표시됩니다. 메시지는 사용자에게 모달 대화 상자에도 나타납니다. 대화 상자에는 메시지, 프로젝트 이름 및 문 번호가 Debug.Assert
포함됩니다. 대화 상자에는 다음 세 개의 명령 단추도 포함됩니다.
- 중단: 애플리케이션의 실행이 중지됩니다.
- 다시 시도: 애플리케이션이 디버그 모드로 전환됩니다.
- 무시: 애플리케이션이 진행됩니다. 애플리케이션을 계속하려면 먼저 사용자가 이러한 단추 중 하나를 클릭해야 합니다.
출력 창 이외의 대상으로 클래스의 Debug
출력을 직접 지정할 수도 있습니다. 클래스에는 Debug
수신기 개체를 포함하는 라는 Listeners
컬렉션이 있습니다. 각 수신기 개체는 출력을 Debug
모니터링하고 출력을 지정된 대상으로 전달합니다. 컬렉션의 각 수신기는 Listeners
클래스가 생성하는 모든 출력을 Debug
받습니다. 클래스를 TextWriterTraceListener
사용하여 수신기 개체를 정의합니다. 해당 생성자를 통해 클래스의 대상을 TextWriterTraceListener
지정할 수 있습니다. 몇 가지 가능한 출력 대상은 다음과 같습니다.
- 속성을 사용하여 콘솔 창입니다
System.Console.Out
. - 문을 사용하여 텍스트 파일입니다
System.IO.File.CreateText("FileName.txt"))
.
개체를 만든 후 출력을 TextWriterTraceListener
받으 Debug
려면 컬렉션에 개체를 Debug.Listeners
추가해야 합니다.
Debug 클래스를 사용하여 샘플 만들기
Visual Basic .NET을 사용하여 conInfo라는 새 콘솔 애플리케이션 프로젝트를 만듭니다. 라는
Module1
퍼블릭 모듈은 기본적으로 프로젝트에 추가됩니다.제품에 대한 정보를 포함하도록 변수를 초기화하려면 다음
Dim
문을 추가합니다.Dim sProdName As String = "Widget" Dim iUnitQty As Integer = 100 Dim dUnitCost As Decimal = 1.03
클래스가 메서드의 첫 번째 입력 매개 변수로 생성하는 메시지를 지정합니다
WriteLine
. Ctrl+Alt+O 키 조합을 눌러 출력 창이 표시되는지 확인합니다.Debug.WriteLine("Debug Information-Product Starting ")
가독성을 위해 메서드를
Indent
사용하여 출력 창에서 후속 메시지를 들여쓰기합니다.Debug.Indent()
선택한 변수의 콘텐츠를 표시하려면 다음과 같이 메서드를
WriteLine
사용합니다.Debug.WriteLine("The product name is " & sProdName) Debug.WriteLine("The available units on hand are " & iUnitQty) Debug.WriteLine("The per unit cost is " & dUnitCost)
메서드를
WriteLine
사용하여 기존 개체의 네임스페이스 및 클래스 이름을 표시할 수도 있습니다. 예를 들어 다음 코드는 출력 창에System.Xml.XmlDocument
네임스페이스를 표시합니다.Dim oxml As New System.Xml.XmlDocument() Debug.WriteLine(oxml)
출력을 구성하려면 범주를 메서드의 선택적 두 번째 입력 매개 변수로 포함할
WriteLine
수 있습니다. 범주를 지정하는 경우 출력 창 메시지의 형식은 "category: message"입니다. 예를 들어 다음 코드의 첫 번째 줄에는 출력 창에 "필드: 제품 이름은 위젯"이 표시됩니다.Debug.WriteLine("The product name is " & sProdName, "Field") Debug.WriteLine("The units on hand are " & iUnitQty, "Field") Debug.WriteLine("The per unit cost is " & dUnitCost, "Field") Debug.WriteLine("Total Cost is" & iUnitQty * dUnitCost, "Calc")
출력 창은 지정된 조건이 클래스의
Debug
메서드를 사용하여WriteLineIf
true로 평가되는 경우에만 메시지를 표시할 수 있습니다. 계산할 조건은 메서드의 첫 번째 입력 매개 변수WriteLineIf
입니다. 의WriteLineIf
두 번째 매개 변수는 첫 번째 매개 변수의 조건이 true로 평가되는 경우에만 표시되는 메시지입니다.Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear") Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear")
지정된 조건이 false로 평가되는 경우에만 출력 창에 메시지가 표시되도록 클래스의
Debug
Assert 메서드를 사용합니다.Debug.Assert(dUnitCost > 1, "Message will NOT appear") Debug.Assert(dUnitCost < 1, "Message will appear")
TextWriterTraceListener
콘솔 창() 및 Output.txt (tr2
tr1
)라는 텍스트 파일의 개체를 만든 다음 컬렉션에 각 개체를Debug
Listeners
추가합니다.Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New _ TextWriterTraceListener(System.IO.File.CreateText("Output.txt")) Debug.Listeners.Add(tr2)
가독성을 위해 메서드를
Unindent
사용하여 클래스가 생성하는 후속 메시지에Debug
대한 들여쓰기를 제거합니다. 및 메서드를Indent
Unindent
함께 사용하면 판독기가 출력을 그룹으로 구분할 수 있습니다.Debug.Unindent() Debug.WriteLine("Debug Information-Product Ending")
각 수신기 개체가 모든 출력을 받도록 하려면 클래스 버퍼에
Flush
대한 메서드를Debug
호출합니다.Debug.Flush()
Trace 클래스 사용
클래스를 Trace
사용하여 애플리케이션 실행을 모니터링하는 메시지를 생성할 수도 있습니다. 및 Debug
클래스는 Trace
다음과 같은 동일한 메서드를 공유하여 출력을 생성합니다.
WriteLine
WriteLineIf
Indent
Unindent
Assert
Flush
및 클래스를 Trace
Debug
동일한 애플리케이션에서 개별적으로 또는 함께 사용할 수 있습니다. 디버그 솔루션 구성 프로젝트에서 및 Debug
출력이 모두 Trace
활성 상태입니다. 프로젝트는 이러한 두 클래스에서 모든 수신기 개체로 출력을 생성합니다. 그러나 릴리스 솔루션 구성 프로젝트는 클래스의 Trace
출력만 생성합니다. 릴리스 솔루션 구성 프로젝트는 클래스 Debug
메서드 호출을 무시합니다.
Trace.WriteLine("Trace Information-Product Starting ")
Trace.Indent()
Trace.WriteLine("The product name is " & sProdName)
Trace.WriteLine("The product name is " & sProdName, "Field")
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear")
Trace.Assert(dUnitCost > 1, "Message will NOT appear")
Trace.Unindent()
Trace.WriteLine("Trace Information-Product Ending")
Trace.Flush()
Console.ReadLine()
작동하는지 확인합니다.
디버그가 현재 솔루션 구성인지 확인합니다.
솔루션 탐색기 창이 표시되지 않으면 Ctrl+Alt+L 키 조합을 눌러 이 창을 표시합니다.
conInfo를 마우스 오른쪽 단추로 클릭한 다음 속성을 클릭합니다.
conInfo 속성 페이지의 왼쪽 창에 있는 구성 폴더 아래에서 화살표가 디버깅을 가리키는지 확인합니다.
구성 폴더 위의 구성 드롭다운 목록 상자에서 활성(디버그) 또는 디버그를 클릭한 다음 확인을 클릭합니다.
Ctrl+Alt+O를 눌러 출력 창을 표시합니다.
F5 키를 눌러 코드를 실행합니다. 어설션 실패 대화 상자가 나타나면 무시를 클릭합니다.
콘솔 창에서 Enter 키를 누릅니다. 프로그램이 완료되고 출력 창에 다음 출력이 표시됩니다.
Debug Information-Product Starting The product name is Widget The available units on hand are 100 The per unit cost is 1.03 System.Xml.XmlDocument Field: The product name is Widget Field: The units on hand are 100 Field: The per unit cost is 1.03 Calc: Total cost is 103 This message WILL appear ---- DEBUG ASSERTION FAILED ---- ---- Assert Short Message ---- Message will appear ---- Assert Long Message ---- at Module1.Main() C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\conInfo\Module1.vb(29) The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending
콘솔 창과 Output.txt 파일에는 다음 출력이 표시됩니다.
(The Output.txt file is located in the same directory as the conInfo executable, conInfo.exe. Normally this is the \bin folder of where the project source has been stored. By default that would be C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin) The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending
전체 코드 목록
Module Module1
Sub Main()
Dim sProdName As String = "Widget"
Dim iUnitQty As Integer = 100
Dim dUnitCost As Decimal = 1.03
Debug.WriteLine("Debug Information-Product Starting ")
Debug.Indent()
Debug.WriteLine("The product name is " & sProdName)
Debug.WriteLine("The available units on hand are " & iUnitQty)
Debug.WriteLine("The per unit cost is " & dUnitCost)
Dim oxml As New System.Xml.XmlDocument()
Debug.WriteLine(oxml)
Debug.WriteLine("The product name is " & sProdName, "Field")
Debug.WriteLine("The units on hand are " & iUnitQty, "Field")
Debug.WriteLine("The per unit cost is " & dUnitCost, "Field")
Debug.WriteLine("Total cost is " & iUnitQty * dUnitCost, "Calc")
Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear")
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear")
Debug.Assert(dUnitCost > 1, "Message will NOT appear")
Debug.Assert(dUnitCost < 1, "Message will appear")
Dim tr1 As New TextWriter`Trace`Listener(System.Console.Out)
Debug.Listeners.Add(tr1)
Dim tr2 As New _
TextWriterTraceListener(System.IO.File.CreateText("Output.txt"))
Debug.Listeners.Add(tr2)
Debug.WriteLine("The product name is " & sProdName)
Debug.WriteLine("The available units on hand are " & iUnitQty)
Debug.WriteLine("The per unit cost is " & dUnitCost)
Debug.Unindent()
Debug.WriteLine("Debug Information-Product Ending")
Debug.Flush()
Trace.WriteLine("`Trace` Information-Product Starting ")
Trace.Indent()
Trace.WriteLine("The product name is " & sProdName)
Trace.WriteLine("The product name is " & sProdName, "Field")
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear")
Trace.Assert(dUnitCost > 1, "Message will NOT appear")
Trace.Unindent()
Trace.WriteLine("Trace Information-Product Ending")
Trace.Flush()
Console.ReadLine()
End Sub
End Module
문제 해결
솔루션 구성 유형이 Release
Debug
이면 클래스 출력이 무시됩니다.특정 대상에 대한 클래스를
TextWriterTraceListener
만든 후 및TextWriterTraceListener
클래스에서Trace
출력을Debug
받습니다. 이는 의 메서드Trace
를 사용하는지 또는 클래스를 클래스에Add
Debug
추가할TextWriterTraceListener
Listeners
지 여부에 관계없이 발생합니다.및
Debug
클래스에서Trace
동일한 대상에 대해 Listener 개체를 추가하면 출력의 생성 여부에Debug
Trace
관계없이 각 출력 줄이 중복됩니다.Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New TextWriterTraceListener(System.Console.Out) Trace.Listeners.Add(tr2)
참조
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기