본문 바로가기

공방/학습 일지

[ .NET ] 앱 디버깅

Visual Studio Code 디버거를 사용하여 대화형으로 .NET 앱 디버깅

중단점(단축키 F9) : 중단하려는 줄에서 단축키를 누르거나 줄 번호 왼쪽을 클릭
중단점 편집 : 마우스로 중단점을 추가하거나 기존의 중단점을 편집하여 조건을 입력할 수 있음
[tip] 통합 터미널 설정하기
vscode/launch.json을 열기
console 설정을 internalConsole 에서 integratedTerminal 로 아래와 같이 변경


"console": "integratedTerminal",

 

TRACE 및 DEBUG 상수 정의

속성 그룹의 프로젝트 파일에 DefineConstants 항목을 추가하면 제어할 수 있다.

기본으로 DEBUG 상수가 정의되는데, 아래는 DEBUG, Release 구성 모두에 대해 TRACE를 활성화한 예이다.

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

 

출력 창에 정보 쓰기

Console.WriteLine("System.Console 사용");			// 임시로 콘솔에 찍어볼 때 사용
Trace.WriteLine("System.Diagnostics.Trace 사용");		// 블드 로그에 주로 사용
Debug.WriteLine("System.Diagnostics.Debug 사용");		// 디버그할 때만 사용

 

조건부 추적

여러가지 방법으로 조건을 걸어줄 수 있다.

if(count == 0) Debug.WriteLine("");		// 조건문 사용
Debug.WriteLine(count == 0, "");		// WriteLineIf 문 사용
System.Diagnostics.Trace.WriteIf(false, "");
System.Diagnostics.Debug.WriteIf(false, "");

// 특정 조건이 존재하는지 확인
int IntegerDivide(int dividend, int divisor)
{
    Debug.Assert(divisor != 0, $"nameof(divisor) is 0 and will cause an exception.");
    return dividend / divisor;
}