c # .net. core to create and publish some configuration app.config windows service, log4net peizhi

1 .net framwork create a windows forms application, write your own regular tasks

 

 

// log4net reads the configuration file from the configuration
[assembly: log4net.Config.XmlConfigurator (ConfigFileExtension = " config", Watch = true)]

-----------------------------app.config----------

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<!-- Set root logger level to ERROR and its appenders -->
<root>
<level value="ALL" />
<appender-ref ref="LogFile" />
</root>
<!-- Print only messages of level DEBUG or above in the packages -->
<logger name="logApp">
<level value="ALL" />
</logger>
<appender name="LogFile" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="Log/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
<appSettings>
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>

 

--------transfer----------

 private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

log.Info ( "write the contents of the log");

2 .net core 2.2 build console application,

Then add a new item c # windows service related to the introduction nugit dll

 

 The introduction of log4net dll

app.config related system node deleted, leaving only the configuration node, the program will automatically load app.config, If it does need to right the main program editor as follows:

 

 

 

 Add Nodes

<ItemGroup>
<Content Include="App.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

Log4net here again next debug log can be written to the specified directory, if released, added to the service, the logs are written to the log system32 directory, where the need to change the configuration and under the code:

 

  <param name="File" type="log4net.Util.PatternString" value="%property{LogsDirectory}\Log\" />

 

 

  log4net.GlobalContext.Properties [ "LogsDirectory"] = AppDomain.CurrentDomain.BaseDirectory; // specified directory service log

Services to write the log after this release will write to the specified destination folder in the next. . . Engage in two days to thoroughly understand, forgive me is white, I hope you see will be helpful.

 

Guess you like

Origin www.cnblogs.com/zhang-wenbin/p/12666672.html