1

I've enabled logging on my desktop application by configuring app.config

<system.diagnostics>
  <trace autoflush="true" indentsize="4">
    <listeners>
      <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="application.log" />
      <remove name="Default" />
    </listeners>
  </trace>
</system.diagnostics>  

And then I can log on my application with a line like this

System.Diagnostics.Trace.TraceInformation("Start tailing file: {0}", watcher.FileLocation);

Now I want to enable the log file ONLY when I start application on Debug mode, because I don't want to distribute my package with log enabled.

Is there any way to configure it?

Fabrizio Stellato
  • 1,727
  • 21
  • 52
  • 1
    JFYI: take a look at log4net for c# https://www.codeproject.com/Articles/140911/log-net-Tutorial it's really easy and there are a lot of tutorials :) – Matthias Burger Sep 11 '17 at 15:03
  • I'd recommend [Slow Cheetah](https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.SlowCheetah-XMLTransforms) and to create a .Debug.Config and .Release.config – Liam Sep 11 '17 at 15:04
  • It already works this way. Project > Properties > Build tab, switch to the Release configuration and untick the "Define TRACE constant" checkbox. – Hans Passant Sep 11 '17 at 15:46
  • May I use slow cheetah with VS Community edition? Because I can't see add transform option – Fabrizio Stellato Sep 12 '17 at 05:05

2 Answers2

0

You can use conditional debug,

#if DEBUG
    //Code to execute only in debug mode    
#endif

or you can decorate method with Condition like below,

[Conditional("DEBUG")]
void RunOnlyWhendebug()
{
}
Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • 1
    I thought this but I think the OP wants to change the config file, not scatter these throughout his code. – Liam Sep 11 '17 at 15:05
  • Either he can return a bool from conditional function to see debug enabled or can create a common function to invoke from all the logging code. I think one or the other it needs a bit refactoring the code – Vijayanath Viswanathan Sep 11 '17 at 15:09
  • Yes, I already know of this pre processor directive, however I don't like it very much to put into code – Fabrizio Stellato Sep 11 '17 at 15:09
  • [Web config transformations](https://stackoverflow.com/questions/305447/using-different-web-config-in-development-and-production-environment) are a much cleaner solution – Liam Sep 11 '17 at 15:10
  • Hmm I do agree config tranformation is cleaner if fine to keep seperate configs – Vijayanath Viswanathan Sep 11 '17 at 15:11
-2

You need a configuration transform. That is, a file which will modify values in your configuration files based on what build configuration you specify. For a while they were most commonly seen with web.config files in ASP.NET projects, but with .Net Core and .Net Standard it's easier to set them up for additional configuration files (appsettings.json for example).

I'm not sure what framework you're targeting, but since you specify an app.config file this walkthrough should be helpful.

Necoras
  • 6,743
  • 3
  • 24
  • 45