Start new console app
Install package log4net
Add XML file, name it like "log4netConfiguration.xml" and in properties set "Copy to Output Directory" to "Copy always":
Here notice the name of appender: myConsoleAppender
Code:
using log4net;
using log4net.Repository;
using System.Reflection;
if (!File.Exists("log4netConfiguration.xml"))
throw new Exception("File log4netConfiguration.xml does not exist");
ILoggerRepository loggerRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
log4net.Config.XmlConfigurator.Configure(loggerRepository, new FileInfo("log4netConfiguration.xml"));
var log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.DeclaringType);
log.Debug("test");
To take advantage of a pattern layout as described in this comment.
The main code:
using System.Reflection;
using log4net;
using log4net.Repository;
namespace log4netWinFormsExample;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogIt_Click(object sender, EventArgs e)
{
if (!File.Exists("log4netConfiguration.xml"))
throw new Exception("File log4netConfiguration.xml does not exist");
ILoggerRepository loggerRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
log4net.Config.XmlConfigurator.Configure(loggerRepository, new FileInfo("log4netConfiguration.xml"));
var log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.DeclaringType);
log.Debug("test");
}
}
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMvc().AddNewtonsoftJson();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Controller:
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace WebApi.Controllers;
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// POST api/<ValuesController>
[HttpPost]
public string Post([FromBody] JObject data)
{
return "test";
}
}
Console:
using System.Text;
Console.WriteLine("************* POST *************");
HttpClient httpClientPost = new HttpClient();
Task<HttpResponseMessage> httpResponseMessage = httpClientPost.PostAsync(@"https://localhost:7037/api/Values"
, new StringContent(@"{""additionalProp1"":[""string""],""additionalProp2"":[""string""],""additionalProp3"":[""string""]}"
, Encoding.UTF8
, "application/json"));
Task<string> httpClientPostResult = httpResponseMessage.Result.Content.ReadAsStringAsync();
Console.WriteLine(httpClientPostResult.Result);
Example download from here. Example contains also dummy Web Api for test purposes.
UPDATE: Just a short notice, correct way to use HttpClient synchronously:
var task = Task.Run(() => myHttpClient.GetAsync(someUrl));
task.Wait();
var response = task.Result;
For the article Writing Custom Control in new WPF XAML Designer I wrote the example app where I needed automatically to restore NuGet package. Write NuGet.Config file in folder where is *.sln file, and write it like: