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

비밀번호

댓글 작성자
 




... 31  32  33  34  35  [36]  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
13041정성태4/28/202214597개발 환경 구성: 642. Informix 데이터베이스 docker 환경 구성
13040정성태4/27/202214227VC++: 156. 비주얼 스튜디오 - Linux C/C++ 프로젝트에서 openssl 링크하는 방법
13039정성태4/27/202217350.NET Framework: 1999. C# - Playwright를 이용한 간단한 브라우저 제어 실습
13038정성태4/26/202213521오류 유형: 806. twine 실행 시 ConfigParser.ParsingError: File contains parsing errors: /root/.pypirc
13037정성태4/25/202214307.NET Framework: 1998. Azure Functions를 사용한 간단한 실습
13036정성태4/24/202215250.NET Framework: 1997. C# - nano 시간을 가져오는 방법 [2]
13035정성태4/22/202216508Windows: 204. Windows 10부터 바뀐 QueryPerformanceFrequency, QueryPerformanceCounter
13034정성태4/21/202214060.NET Framework: 1996. C# XingAPI - 주식 종목에 따른 PBR, PER, ROE, ROA 구하는 방법(t3320, t8430 예제)파일 다운로드1
13033정성태4/18/202215198.NET Framework: 1195. C# - Thread.Yield와 Thread.Sleep(0)의 차이점(?)
13032정성태4/17/202215376오류 유형: 805. Github의 50MB 파일 크기 제한 - warning: GH001: Large files detected. You may want to try Git Large File Storage
13031정성태4/15/202215086.NET Framework: 1194. C# - IdealProcessor와 ProcessorAffinity의 차이점
13030정성태4/15/202213593오류 유형: 804. 정규 표현식 오류 - Quantifier {x,y} following nothing.
13029정성태4/14/202215028Windows: 203. iisreset 후에도 이전에 설정한 전역 환경 변수가 w3wp.exe에 적용되는 문제
13028정성태4/13/202215150.NET Framework: 1193. (appsettings.json처럼) web.config의 Debug/Release에 따른 설정 적용
13027정성태4/12/202215015.NET Framework: 1192. C# - 환경 변수의 변화를 알리는 WM_SETTINGCHANGE Win32 메시지 사용법파일 다운로드1
13026정성태4/11/202216360.NET Framework: 1191. C 언어로 작성된 FFmpeg Examples의 C# 포팅 전체 소스 코드 [3]
13025정성태4/11/202215636.NET Framework: 1190. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 vaapi_encode.c, vaapi_transcode.c 예제 포팅
13024정성태4/7/202213876.NET Framework: 1189. C# - 런타임 환경에 따라 달라진 AppDomain.GetCurrentThreadId 메서드
13023정성태4/6/202214471.NET Framework: 1188. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 transcoding.c 예제 포팅 [3]
13022정성태3/31/202214025Windows: 202. 윈도우 11 업그레이드 - "PC Health Check"를 통과했지만 여전히 업그레이드가 안 되는 경우 해결책
13021정성태3/31/202215850Windows: 201. Windows - INF 파일을 이용한 장치 제거 방법
13020정성태3/30/202214094.NET Framework: 1187. RDP 접속 시 WPF UserControl의 Unloaded 이벤트 발생파일 다운로드1
13019정성태3/30/202214556.NET Framework: 1186. Win32 Message를 Code로부터 메시지 이름 자체를 텍스트로 구하고 싶다면?파일 다운로드1
13018정성태3/29/202215299.NET Framework: 1185. C# - Unsafe.AsPointer가 반환한 포인터는 pinning 상태일까요? [5]
13017정성태3/28/202214260.NET Framework: 1184. C# - GC Heap에 위치한 참조 개체의 주소를 알아내는 방법 - 두 번째 이야기 [3]
13016정성태3/27/202215968.NET Framework: 1183. C# 11에 추가된 ref 필드의 (우회) 구현 방법파일 다운로드1
... 31  32  33  34  35  [36]  37  38  39  40  41  42  43  44  45  ...