简体   繁体   中英

Environment variable does not get read?

I have a console application in Visual studio. i have log4net stuff with smtp appender in my app.config file. I have set environment variable in my code (ie my email address) and trying to reference this environment variable to send email. However log4net doesn't seem to read this value when the application is run.

My log4net:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="smtp" type="log4net.Appender.SmtpAppender">  
            <param name="to" value="${EmailAddress}" />
            <param name="from" value="myapp@gmail.com" />
            <param name="subject" value="testing app" /> 
            <param name="smtpHost" value="<smtp host name>" />
            <param name="bufferSize" value="1" />
            <param name="lossy" value="false" />
            <param name="Threshold" value="ERROR"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%d{ISO8601} [%t] [%-5p] %c - %m%n" />
            </layout>
        </appender>
        <root>
            <priority value="ALL" />
            <appender-ref ref="smtp" />            
        </root>
    </log4net>
</Configuration>

In my console app, I have set environment variable something like this:

Environment.SetEnvironmentVariable("EmailAddress", "testApp@gmail.com", EnvironmentVariableTarget.Process);

Does anyone know how can I make it work? Thanks.

I see an old release notes for log4net that says you use an environment variable with the money sign and brackets. But I've never used that style before.

${AppData}

I've used the percent style for accessing special folders and it works. Try it like this with your custom environment variable.

%AppData%

But, I do have to ask why you're putting a "To:" email address in an environment variable? Can't you use something a bit more modern like the built in log4net Contexts ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM