Skip to content

Commit

Permalink
Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
AscarGb committed Nov 20, 2018
1 parent 53671ab commit 44384f9
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
6 changes: 5 additions & 1 deletion cmanager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace cmanager
{
Expand Down Expand Up @@ -37,6 +38,8 @@ static int Main(string[] args)
services.AddTransient<RenameRoleVerb>();
services.AddSingleton<ConsoleWriter>();

services.AddLogging(configure => configure.AddConsole())
.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Information);

var connectionString = configuration.GetConnectionString("DefaultConnection");
var dbType = configuration.GetSection("DbType").Get<string>();
Expand Down Expand Up @@ -72,7 +75,8 @@ static int Main(string[] args)
}
catch (Exception e)
{
provider.GetService<ConsoleWriter>().WriteText(e.Message);
var logger = provider.GetRequiredService<ILogger<Program>>();
logger.LogError(e, "An error occurred");
return 1;
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmanager/Verbs/AddRoleVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public override int Execute(AddRoleOptions opts)

if (user == null)
{
_consoleWriter.WriteText("User not found");
return 1;
throw new Exception("User not found");
}

foreach (var r in opts.Roles)
Expand Down
3 changes: 1 addition & 2 deletions cmanager/Verbs/DeleteUserVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ override public int Execute(DeleteUserOptions opts)
var u = _userManager.FindByNameAsync(opts.UserName).Result;
if (u == null)
{
_consoleWriter.WriteText("Not found");
return 1;
throw new Exception("User not found");
}

var result = _userManager.DeleteAsync(u).Result;
Expand Down
3 changes: 1 addition & 2 deletions cmanager/Verbs/FindUserVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ override public int Execute(FindUserOptions opts)
var u = _userManager.FindByNameAsync(opts.UserName).Result;
if (u == null)
{
_consoleWriter.WriteText("Not found");
return 1;
throw new Exception("User not found");
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions cmanager/Verbs/RenameRoleVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public override int Execute(RenameRoleOptions opts)

if (role == null)
{
_consoleWriter.WriteText("Role not found");
return 1;
throw new Exception("Role not found");
}

role.Name = opts.NewRoleName;
Expand Down
1 change: 1 addition & 0 deletions cmanager/Verbs/RolesListVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public RolesListVerb(
public override int Execute()
{
_consoleWriter.WriteText(string.Join(", ", _roleManager.Roles));

return 0;
}
}
Expand Down
1 change: 1 addition & 0 deletions cmanager/cmanager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
</ItemGroup>

Expand Down

0 comments on commit 44384f9

Please sign in to comment.