0

I found documentation that you can't log directly in your Program.cs file but that it would be possible with a 3rd party library like Serilog. Microsoft Link

After finding out you can't break in your program.cs I tried to add logging to my program.cs. This doesn't seem to be working in Blazor WASM.

I tried it in a DotNet Core WebApi and it works just fine there.

The code I used in both the API as Blazor project:

Log.Logger = new LoggerConfiguration()
               .WriteTo.File(@"hello\world\setup.txt")
               .CreateLogger();

Log.Fatal("Hello friend!");

To be fair this seems like a bug. I'm looking for a working sample.

0

1 Answer 1

3

A Blazor WASM (Web Assembly) project runs in the user's browser, not on a server. This means "normal" things you're used to, like writing to a file, isn't possible.

You can log with Serilog or Microsoft's ILogger, but you need to send logs to an internet location. However, because WASM means that you transport your project's dll to the user's browser for them to execute, anyone can decompile your project and extract any sensitive connection details (urls, credentials, etc).

You may want to look into Telemetry rather than logging, such as with Application Insights (though it seems that is still in the works).

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

2 Comments

Regarding Application Insights I've used this and it worked out fine.
I was not clear enough with my question. I was looking for a way to log certain properties in my program.cs file to validate the value on startup. I've found documentation that this is currently impossible. As your answer is actually answering the question I'll accept it.

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.