log4net이 작동하지 않습니다
내 web.config 에이 구성이 있습니다.
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="mylog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="" />
<param name="Footer" value="" />
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
그러나 log4net이 작동하지 않습니다. 내 프로젝트가 정상적으로 컴파일되고 디버깅 오류가 발생하지 않습니다. 내가 말한 줄 log.debug("somemessage")
은 제대로 실행되지만 mylog.log
파일을 찾을 수 없으므로 어디에 있습니까?
이 유형의 것 중 하나 XmlConfigurator
는 다음 줄을 다음과 같이 어셈블리에 추가하여 속성을 어셈블리에 추가하는 것입니다 AssemblyInfo.cs
.
[assembly: log4net.Config.XmlConfigurator]
그렇지 않으면 log4net이 활성화되지 않습니다.
log4net이 전혀 로깅되지 않거나 파일이 원하는 곳에서 끝나지 않는 것 같습니다.
먼저 실제로 전화를 했습니까
XmlConfigurator.Configure()
코드 어딘가에? 위의 XML 스 니펫이 애플리케이션 구성 파일에있는 경우이 호출로 트릭을 수행합니다. xml 스 니펫이 자체 파일에 있으면 파일 .Configure(string)
경로를 취하는 오버로드 를 사용해야 합니다. 이 호출 (또는 Kirk Woll에 의해 언급 된 어셈블리 레벨 속성)이 없으면 log4net은 전혀 로깅되지 않습니다.
이 작업이 모두 완료되었다고 생각하고 log4net이 로깅 중이어야한다면 추가 디버깅을하는 동안 로그 파일에 대한 정규화 된 경로를 입력해야합니다. 즉, 파일이 위치를 확인하게됩니다 해야 합니다.
또 다른 작은 문제가 있습니다. http://logging.apache.org/log4net/release/manual/configuration.html#dot-config
the [assembly: log4net.Config.XmlConfigurator]
method doesn't work with app.config. If you configure log4net from app.config, you must use the log4net.Config.XmlConfigurator.Configure()
method.
Here is my checklist for when log4net is proving to be recalcitrant:
- ensure that log4net.config file is copied to the bin\ folder when building (set to 'Copy if newer' in compiler)
- when dealing with installed code, ensure that the log4net.config came along for the ride (set to 'Content' in compiler)
- ensure that the user that the process runs as has write rights to the folder where the logs are to be written
- if in doubt, give promiscuous permissions to c:\temp\ and get everything to log to there ()
- fire up Sysinternal/Dbgview.exe to see if that tells you anything
These are the steps which eventually got my file logging working:
- -Check AssemblyInfo.cs contains the following attribute. [assembly: log4net.Config.XmlConfigurator]. This loads log4net.
- Check the log directory has write permissions.
- Check the logger has a format specified. This is done by checking each element in your configuration has a layout element specified. Eg:
<appender name="MainLogger"... <layout type="log4net.Layout.SimpleLayout"/>
- Finally, try turning on log4net internal logging to enable console logging and checking the console. To do this, add
<add key="log4net.Internal.Debug" value="true"/>
to yourappSettings
.
For an ASP.NET MVC project adding
log4net.Config.XmlConfigurator.Configure();
to the Global.asax.cs also helps:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
log4net.Config.XmlConfigurator.Configure();
}
}
I've had experiences where logging systems fail silently without raising exceptions. I suppose this makes sense because if the logger is logging errors then how can it log an error that it's unable to perform logging?
So if the file isn't created on disk, start by looking into file system permissions to ensure the user your application is running under can write a new file to that disk location.
For testing purposes you might want to manually create the file on disk that should be written to and open up permissions for everybody to write to it. If the logger starts writing to it then you know it's permission based rather than configuration based.
I tried all of the above but nothing worked. Adding this line in app.config's configSections
section worked for me.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
Ensure that Version
and PublicKeyToken
is correct
<param name="File" value="mylog.log" />
is saying "write mylog.log to actual folder". This means that if your webapp is under IIS, then log will be written to C:\inetpub\wwwroot\appname\mylog.log.
If log file is not there, then maybe account under which is app running doesn't have write permission on folder. You can run Process Monitor from SysInternals to see if and where a file is written.
Also run VS in debug mode, to see if any exceptions are thrown (Debug->Exceptions->CLR Exceptions, check Thrown).
In my case I forget to set the log4Net.config file properties as "Content" so the file was not included in the deploy. So pay attention on it:
Compile action : Content
Unfortunately none of the above helped. Explicit configuration in the class to be logged additionaly to the previous settings suggestions did the trick for me.
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
log4net.Config.XmlConfigurator.Configure(new FileInfo(assemblyFolder + "/log4net.config"));
Also after some hours, I figured out why it wasn't working for me...
i had:
public static class Program
{
private static CommunicationManager _bcScanner = new CommunicationManager();
private static ILog _log = LogManager.GetLogger(typeof(Program));
private static SocketServer socketListener;
but it should be:
public static class Program
{
private static ILog _log = LogManager.GetLogger(typeof(Program));
private static CommunicationManager _bcScanner = new CommunicationManager();
private static SocketServer socketListener;
So make sure the ILog is on the first line....
참고URL : https://stackoverflow.com/questions/3898218/log4net-not-working
'programing tip' 카테고리의 다른 글
파이썬을 사용하여 문자열에서 숫자를 제외한 문자를 제거 하시겠습니까? (0) | 2020.07.13 |
---|---|
PHP를 사용하여 CSV 파일을 구문 분석하는 방법 (0) | 2020.07.13 |
안전한 지역 삽입 상하 높이 확보 (0) | 2020.07.13 |
Gradle 빌드 유형별로 앱 이름을 변경하는 방법 (0) | 2020.07.13 |
AppCompat 라이브러리에서 SwitchCompat의 색상을 변경하는 방법 (0) | 2020.07.13 |