1

Where Asp.Net Core stored log file that ILogger write ?

Is it any standard way to change log files location ?

I don't want to use any pacakges like https://www.nuget.org/packages/serilog.extensions.logging.file

2
  • Which ILogger method are you calling? Commented Mar 21, 2017 at 21:24
  • LogInformation for example Commented Mar 22, 2017 at 5:03

1 Answer 1

2

Built-in logging providers for ASP.NET Core applications are:

None of these classes can be used for logging to the file by default.
However, you can use tracing listener, which can be set up for file usage, like this:

public void Configure(IApplicationBuilder app,
    IHostingEnvironment env,
    ILoggerFactory loggerFactory)
{
    loggerFactory
        .AddDebug();

    // add Trace Source logging
    var testSwitch = new SourceSwitch("sourceSwitch", "Logging Sample");
    testSwitch.Level = SourceLevels.Warning;
    loggerFactory.AddTraceSource(testSwitch,
        new TextWriterTraceListener("MyLogName.txt"));

As you can see, here you can filter your messages, right now it will write down all the warnings and higher messages. Also you can setup a filter for listener:

var testSwitch = new SourceSwitch("sourceSwitch", "Logging Sample");
var listener = new TextWriterTraceListener("MyLogName.txt");
listener.Filter = new SourceFilter("YourFilterName");
loggerFactory.AddTraceSource(testSwitch, listener);

In consumer classes this can be used as:

logger = loggerFactory.CreateLogger("YourFilterName");

Related articles:

Sign up to request clarification or add additional context in comments.

1 Comment

There is one more logging provider -- DiagnosticSource github.com/dotnet/runtime/blob/master/src/libraries/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.