asp.net core mysql conblogs,asp.net core使用日志组件NLog和Log4Net实例教程

Nlog相关资料:

1 nuget安装NLog.Web.AspNetCore

2 在web应用程序Program.cs中对照修改代码

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

WebHost.CreateDefaultBuilder(args)

.UseStartup()

.ConfigureLogging(logging =>

{

logging.ClearProviders();

logging.SetMinimumLevel(LogLevel.Trace);

})

.UseNLog();

1

2

3

4

5

6

7

8

9

publicstaticIWebHostBuilderCreateWebHostBuilder(string[]args)=>

WebHost.CreateDefaultBuilder(args)

.UseStartup()

.ConfigureLogging(logging=>

{

logging.ClearProviders();

logging.SetMinimumLevel(LogLevel.Trace);

})

.UseNLog();

3 在相应的控制器中修改

private readonly ILogger _logger;

public HomeController(ILogger logger)

{

_logger = logger;

}

public IActionResult Index()

{

_logger.LogInformation("hello--index");

return View();

}

1

2

3

4

5

6

7

8

9

10

11

12

privatereadonlyILogger_logger;

publicHomeController(ILoggerlogger)

{

_logger=logger;

}

publicIActionResultIndex()

{

_logger.LogInformation("hello--index");

returnView();

}

4 在web应用程序中添加nlog.config配置文件,并设置为较新则复制或始终复制

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

autoReload="true"

internalLogLevel="Info"

internalLogFile="c:\temp\internal-nlog.txt">

layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

autoReload="true"

internalLogLevel="Info"

internalLogFile="c:\temp\internal-nlog.txt">

layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"/>

layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}"/>

Log4Net相关资料:

1 nuget安装Microsoft.Extensions.Logging.Log4Net.AspNetCore

2 修改web应用程序program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

WebHost.CreateDefaultBuilder(args)

.UseStartup()

.ConfigureLogging((hostingContext, logging) =>

{

// The ILoggingBuilder minimum level determines the

// the lowest possible level for logging. The log4net

// level then sets the level that we actually log at.

logging.AddLog4Net();

logging.SetMinimumLevel(LogLevel.Debug);

});

1

2

3

4

5

6

7

8

9

10

11

publicstaticIWebHostBuilderCreateWebHostBuilder(string[]args)=>

WebHost.CreateDefaultBuilder(args)

.UseStartup()

.ConfigureLogging((hostingContext,logging)=>

{

// The ILoggingBuilder minimum level determines the

// the lowest possible level for logging. The log4net

// level then sets the level that we actually log at.

logging.AddLog4Net();

logging.SetMinimumLevel(LogLevel.Debug);

});

3 在相应的控制器中修改

private readonly ILogger _logger;

public HomeController(ILogger logger)

{

_logger = logger;

}

public IActionResult Index()

{

_logger.LogInformation("hello--index");

return View();

}

1

2

3

4

5

6

7

8

9

10

11

12

privatereadonlyILogger_logger;

publicHomeController(ILoggerlogger)

{

_logger=logger;

}

publicIActionResultIndex()

{

_logger.LogInformation("hello--index");

returnView();

}

4 在Web应用程序中添加log4net.config,并设置为较新复制或始终复制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

运行程序,会在Web应用程序目录下生成日志文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值