1 Star 0 Fork 0

麦壳饼 / NetEscapades.Extensions.Logging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

NetEscapades.Extensions.Logging

Build status NuGet RollingFile MyGet RollingFile CI

NetEscapades.Extensions.Logging.RollingFile

A rolling file provider for ASP.NET Core 2.1 Microsoft.Extensions.Logging, the logging subsystem used by ASP.NET Core. Writes logs to a set of text files, one per day.

Getting Started

First Install the NetEscapades.Extensions.Logging.RollingFile package from NuGet, either using powershell:

Install-Package NetEscapades.Extensions.Logging.RollingFile

or using the .NET CLI:

dotnet add package NetEscapades.Extensions.Logging.RollingFile

Next configure the provider by calling AddFile() on an ILoggingBuilder during logger configuration in Program.cs.

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(builder => builder.AddFile()) // <- Add this line
            .UseStartup<Startup>()
            .Build();
}

You can pass additional options to the Add File by passing an Action<FileLoggerOptions>, for example:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(builder => builder.AddFile(options => {
                options.FileName = "diagnostics-"; // The log file prefixes
                options.LogDirectory = "LogFiles"; // The directory to write the logs
                options.FileSizeLimit = 20 * 1024 * 1024; // The maximum log file size (20MB here)
                options.Extension = "txt"; // The log file extension
                options.Periodicity = PeriodicityOptions.Hourly // Roll log files hourly instead of daily.
            })) 
            .UseStartup<Startup>()
            .Build();
}

Finally The provider will create log files prefixed with the FileName, and suffixed with the current date in the yyyyMMddHHmm format (using only the portions up to the selected periodicity, yyyyMMdd for the default of daily).

log-20160631.txt
log-20160701.txt
log-20160702.txt

Logs will look something like the following:

2017-09-01 18:34:18.083 +01:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://localhost:50037/api/values  
2017-09-01 18:34:18.159 +01:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method SampleApp.Controllers.ValuesController.Get (SampleApp) with arguments ((null)) - ModelState is Valid
2017-09-01 18:34:18.161 +01:00 [Information] SampleApp.Controllers.ValuesController: Executed Get action
2017-09-01 18:34:18.165 +01:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor: Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
2017-09-01 18:34:18.192 +01:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action SampleApp.Controllers.ValuesController.Get (SampleApp) in 36.3435ms
2017-09-01 18:34:18.195 +01:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 113.6076ms 200 application/json; charset=utf-8

Credits

This provider is heavily cribbed from the Azure App Service Logging Provider from the ASP.NET team.

The MIT License (MIT) Copyright (c) 2016 andrewlock Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/maikebing/NetEscapades.Extensions.Logging.git
git@gitee.com:maikebing/NetEscapades.Extensions.Logging.git
maikebing
NetEscapades.Extensions.Logging
NetEscapades.Extensions.Logging
master

搜索帮助