Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 3개 있습니다.)
개발 환경 구성: 220. supportedRuntime 설정을 위한 app.config Transformation
; https://www.sysnet.pe.kr/2/0/1662

.NET Framework: 1193. (appsettings.json처럼) web.config의 Debug/Release에 따른 설정 적용
; https://www.sysnet.pe.kr/2/0/13028

VS.NET IDE: 200. C# - app.config 파일의 출력을 Configuration(Debug/Release)에 따라 제어하는 방법
; https://www.sysnet.pe.kr/2/0/13918




C# - app.config 파일의 출력을 Configuration(Debug/Release)에 따라 제어하는 방법

대표적인 예로, Debug / Release 빌드 시 다른 app.config 출력을 원할 때가 있습니다. 예전에도 이와 관련한 예제를 다루긴 했는데요,

supportedRuntime 설정을 위한 app.config Transformation
; https://www.sysnet.pe.kr/2/0/1662

golavr/ConfigurationTransform
; https://github.com/golavr/ConfigurationTransform

아쉽게도 그때 소개한 Configuration Transform 도구가 비주얼 스튜디오 2022 버전을 지원하지 않습니다. github 프로젝트에 들어가 보면, "This work has been superseded by https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration"라는 문구가 나오는데요, 링크를 따라가 보면 그다지 관련 없는 듯한 내용의 "Configuration in .NET" 글이 나옵니다. 이건 제 추측이지만 아마도 닷넷 코어부터 appsettings.json 방식이 새롭게 도입되면서 XML 방식의 app.config 지원을 스스로 포기한 것이 아닌가 싶습니다.

암튼, 이렇게 되면 본연의 XDT Transforms을 사용하거나,

XDT (web.config) Transforms in non-web projects
; https://devblogs.microsoft.com/dotnet/xdt-web-config-transforms-in-non-web-projects-2/

이것을 쉽게 처리해 주는 (웬일인지 마이크로소프트가 별도로 만든) 확장 도구를 사용하면 됩니다.

SlowCheetah
; https://marketplace.visualstudio.com/items?itemName=vscps.SlowCheetah-XMLTransforms-VS2022

microsoft/slow-cheetah
; https://github.com/Microsoft/slow-cheetah

도구 설치 후, 가령 Console Application 프로젝트를 하나 만들고 app.config 파일을 마우스로 우클릭하면 아래와 같이 "Add Transform" 메뉴가 뜹니다.

278679_1_AddTransform.png

이를 선택하면, 솔루션 탐색기의 app.config 파일 노드 하위로 다음과 같이 2개의 Debug/Release 변형 파일이 생성됩니다.

278680_1_TransformFiles.png

테스트를 위해 3개의 파일을 각각 다음과 같이 구성하고,

[app.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="MySetting" value="Hello, World! - (Any)" />
        <add key="DebugSetting" value="1" />
    </appSettings>
</configuration>

[app.Debug.config]
<?xml version="1.0" encoding="utf-8"?>
<!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="MySetting" value="Hello, World! - (Debug)" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
    </appSettings>
</configuration>

[app.Release.config]
<?xml version="1.0" encoding="utf-8"?>
<!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="MySetting" value="Hello, World! - (Release)" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
        <add key="DebugSetting" value="http://contoso.com/" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
    </appSettings>
</configuration>

코드를 작성한 후,

using System.Configuration;

internal class Program
{
    // .NET Core/5+의 경우
    // Install-Package System.Configuration.ConfigurationManager
    static void Main(string[] args)
    {
        string? value = ConfigurationManager.AppSettings["MySetting"];
        Console.WriteLine(value);

        value = ConfigurationManager.AppSettings["DebugSetting"];
        Console.WriteLine(value ?? "(null)");
    }
}

실행해 보면 Debug/Release에 따라 각각 다음과 같은 결과를 얻을 수 있습니다.

// Debug로 빌드한 경우 실행 결과

Hello, World! - (Debug)
1

// Release로 빌드한 경우 실행 결과

Hello, World! - (Release)
(null)

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/2/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13748정성태9/29/20245793닷넷: 2301. C# - BigInteger 타입이 byte 배열로 직렬화하는 방식
13747정성태9/28/20245719닷넷: 2300. C# - OpenSSH의 공개키 파일에 대한 "BEGIN OPENSSH PUBLIC KEY" / "END OPENSSH PUBLIC KEY" PEM 포맷파일 다운로드1
13746정성태9/28/20245727오류 유형: 924. Python - LocalProtocolError("Illegal header value ...")
13745정성태9/28/20245597Linux: 80. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (lldb)
13744정성태9/27/20246042닷넷: 2299. C# - Windows Hello 사용자 인증 다이얼로그 표시하기파일 다운로드1
13743정성태9/26/20246503닷넷: 2298. C# - Console 프로젝트에서의 await 대상으로 Main 스레드 활용하는 방법 [1]
13742정성태9/26/20246794닷넷: 2297. C# - ssh-keygen으로 생성한 ecdsa 유형의 Public Key 파일 해석 [1]파일 다운로드1
13741정성태9/25/20246002디버깅 기술: 202. windbg - ASP.NET MVC Web Application (.NET Framework) 응용 프로그램의 덤프 분석 시 요령
13740정성태9/24/20245806기타: 86. RSA 공개키 등의 modulus 값에 0x00 선행 바이트가 있는 이유(ASN.1 인코딩)
13739정성태9/24/20245947닷넷: 2297. C# - ssh-keygen으로 생성한 Public Key 파일 해석과 fingerprint 값(md5, sha256) 생성 [1]파일 다운로드1
13738정성태9/22/20245693C/C++: 174. C/C++ - 윈도우 운영체제에서의 file descriptor, FILE*파일 다운로드1
13737정성태9/21/20246063개발 환경 구성: 727. Visual C++ - 리눅스 프로젝트를 위한 빌드 서버의 msbuild 구성
13736정성태9/20/20246047오류 유형: 923. Visual Studio Code - Could not establish connection to "...": Port forwarding is disabled.
13735정성태9/20/20246142개발 환경 구성: 726. ARM 플랫폼용 Visual C++ 리눅스 프로젝트 빌드
13734정성태9/19/20245810개발 환경 구성: 725. ssh를 이용한 원격 docker 서비스 사용
13733정성태9/19/20246151VS.NET IDE: 194. Visual Studio - Cross Platform / "Authentication Type: Private Key"로 접속하는 방법
13732정성태9/17/20246213개발 환경 구성: 724. ARM + docker 환경에서 .NET 8 설치
13731정성태9/15/20246831개발 환경 구성: 723. C# / Visual C++ - Control Flow Guard (CFG) 활성화 [1]파일 다운로드2
13730정성태9/10/20246523오류 유형: 922. docker - RULE_APPEND failed (No such file or directory): rule in chain DOCKER
13729정성태9/9/20247292C/C++: 173. Windows / C++ - AllocConsole로 할당한 콘솔과 CRT 함수 연동 [1]파일 다운로드1
13728정성태9/7/20247087C/C++: 172. Windows - C 런타임에서 STARTUPINFO의 cbReserved2, lpReserved2 멤버를 사용하는 이유파일 다운로드1
13727정성태9/6/20247636개발 환경 구성: 722. ARM 플랫폼 빌드를 위한 미니 PC(?) - Khadas VIM4 [1]
13726정성태9/5/20247491C/C++: 171. C/C++ - 윈도우 운영체제에서의 file descriptor와 HANDLE파일 다운로드1
13725정성태9/4/20246237디버깅 기술: 201. WinDbg - sos threads 명령어 실행 시 "Failed to request ThreadStore"
13724정성태9/3/20248097닷넷: 2296. Win32/C# - 자식 프로세스로 HANDLE 상속파일 다운로드1
13723정성태9/2/20248331C/C++: 170. Windows - STARTUPINFO의 cbReserved2, lpReserved2 멤버 사용자 정의파일 다운로드2
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...