简体   繁体   English

环境变量无法读取?

[英]Environment variable does not get read?

I have a console application in Visual studio. 我在Visual Studio中有一个控制台应用程序。 i have log4net stuff with smtp appender in my app.config file. 我的app.config文件中有带有smtp附加程序的log4net东西。 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. 但是,运行应用程序时,log4net似乎不读取该值。

My log4net: 我的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. 我看到了log4net的旧发行说明 ,其中说您使用带有金钱符号和方括号的环境变量。 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 ? 您不能使用一些更现代的东西,例如内置的log4net Contexts吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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