Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to tell if in production or development environment in .NET

I would like to send email differently on production then the development environment, such as sending emails only to me while testing. I know that I can set an application value in the webconfig file or check the url before sending the emails. I would like to know what is the best practice for doing this or if there is some other option that I don't know about that might be better? Any information on this topic would be greatly appreciated.

like image 244
user1572948 Avatar asked Aug 27 '12 03:08

user1572948


2 Answers

I use what's called Preprocessor Directives (in C#). In VB it's called Directives only.

I have a helper method like this:

public static bool IsDebug()
{
    #if DEBUG
        return true;
    #else
        return false;
    #endif
}

Then anywhere in the code one can call this method and during runtime and it'll tell if the code is being run in Debug ( development ) or if it's the Release version ( production ).

Here's the equivalent #If...Then...#Else Directives in (VB).

like image 120
Leniel Maccaferri Avatar answered Nov 13 '22 18:11

Leniel Maccaferri


If you want to only send the email to yourself if it's not Production then do it by changing the email address on the "To" field.

So either change it in the database or config file, wherever you put that value.

The way the application determines if you are in the development server is with the "ASPNETCORE_ENVIRONMENT".

You can check this link, Use multiple environments in ASP.NET Core

like image 26
Rod Talingting Avatar answered Nov 13 '22 19:11

Rod Talingting