开发者

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 (i.e. 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜