如何使用log4net在AppData文件夹中创建文件 [英] How to create a file in the AppData folder using log4net

查看:473
本文介绍了如何使用log4net在AppData文件夹中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在appData文件夹中创建日志文件。路径为C:\Users\MYNAME\AppData\Roaming\Project\My Project\Application。一旦我的项目开始,项目文件夹创建在这个路径硬编码的路径。如何使用log4net在此文件夹中添加我的日志文件?
我在配置文件中进行了更改

How to create the log file in appData folder. The path is C:\Users\MYNAME\AppData\Roaming\Project\My Project\Application. As soon as my project starts, the project folder is created on this path where this path is hard coded. How can I add my log file in this folder using log4net? I have made changes in the config file

<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <!-- Pattern to output the caller's file name and line    number -->
            <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
        </layout>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">

        <file value="${APPDATA}\\Roaming\\Project\\My Project\\Application\\Log.txt"/>
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="10" />
        <layout type="log4net.Layout.PatternLayout">            
            <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="Console" />
        <appender-ref ref="RollingFile" />
    </root>
</log4net>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
    <bindings />
    <client />
</system.serviceModel>
 </configuration>

这不会在此文件夹中创建任何文件。所有权限授予为Administrator。

This doesn't create any files in this folder. And all the permissions are granted being Administrator.

推荐答案

看起来你是以正确的方式做的,但是有些情况下,区别:其他问题

It seems that you are doing it the right way, however there are cases that it seems the casing of the variable makes a difference: An other question on the subject

您可以尝试: $ {AppData} 工作

这篇关于如何使用log4net在AppData文件夹中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆