Write information to different log files

I have the following configurations in my web.config file, but how can I write information in data.txt and general.txt separately in C #?

Can someone provide me some sample code?

 <appender name="GeneralLog" type="log4net.Appender.RollingFileAppender"> <file value="App_Data/Logs/general.txt" /> <appendToFile value="true" /> <maximumFileSize value="2MB" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" /> </layout> </appender> <appender name="DataLog" type="log4net.Appender.RollingFileAppender"> <file value="App_Data/Logs/data.txt" /> <appendToFile value="true" /> <maximumFileSize value="2MB" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" /> </layout> </appender> 
+4
source share
1 answer

In your application, ideally, you should register the same path for both the general and the data, simply using different registrars. In the configuration, you have several options for "routing" incoming log messages to different applications.

The first is the registrars themselves. Depending on how your application is organized in terms of classes and namespaces, you organize your logs in a hierarchical order according to your namespace hierarchy. Then you can have different branches in the hierarchy going to different applications. Read the "Inheritance Level" section here .

In addition to log hierarchies, you can use any name as log names. Therefore, for your general sections of your application, you can use a registrar named "general".

The next thing you can use to control the flow of log messages uses filters . You can filter messages by name, level, properties, etc., Or you can implement your own filters.

Here's an example showing how you could route shared and data to your applications using the log element and filter. We route the root element (which receives all log messages) to the data application. In the data application, we filter out all messages coming from the common registrar.

In addition, we direct the general registrar to the general appender.

 <appender name="GeneralLog" type="log4net.Appender.RollingFileAppender"> ... </appender> <appender name="DataLog" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LoggerMatchFilter"> <loggerToMatch value="general" /> <acceptOnMatch value="false" /> </filter> ... </appender> <root> <level value="DEBUG" /> <appender-ref ref="DataLog" /> </root> <logger name="general"> <level value="WARN" /> <appender-ref ref="GeneralLog" /> </logger> 
+4
source

Source: https://habr.com/ru/post/1303514/


All Articles