❔ Assignment Overview help

How do i make my library configurable
26 Replies
Protagonist
ProtagonistOP2y ago
so i have a framework/library for a 2D game and i need to add somethings to it, one of these things is tracing/logging
public void Hit(Creature enemyCreature)
{
AttackItem equippedAttackItem = GetEquippedAttackItem();

if (equippedAttackItem != null && !enemyCreature.IsDead)
{
int distanceX = this.Position.X - enemyCreature.Position.X;
int distanceY = this.Position.Y - enemyCreature.Position.Y;

if (distanceX <= equippedAttackItem.Range && distanceY <= equippedAttackItem.Range)
{
Console.WriteLine($"{Name} attacks {enemyCreature.Name} with {equippedAttackItem.Name}.");
enemyCreature.RecieveHit(this, equippedAttackItem);
Logger.Log($"{Name} attacks {enemyCreature.Name} with {equippedAttackItem.Name}.");
}
else
{
Console.WriteLine($"{Name} cannot attack {enemyCreature.Name}.");
Logger.Log($"{Name} cannot attack {enemyCreature.Name}.");
}
}
else
{
Console.WriteLine($"{Name} cannot attack {enemyCreature.Name}.");
Logger.Log($"{Name} cannot attack {enemyCreature.Name}.");
}
}
public void Hit(Creature enemyCreature)
{
AttackItem equippedAttackItem = GetEquippedAttackItem();

if (equippedAttackItem != null && !enemyCreature.IsDead)
{
int distanceX = this.Position.X - enemyCreature.Position.X;
int distanceY = this.Position.Y - enemyCreature.Position.Y;

if (distanceX <= equippedAttackItem.Range && distanceY <= equippedAttackItem.Range)
{
Console.WriteLine($"{Name} attacks {enemyCreature.Name} with {equippedAttackItem.Name}.");
enemyCreature.RecieveHit(this, equippedAttackItem);
Logger.Log($"{Name} attacks {enemyCreature.Name} with {equippedAttackItem.Name}.");
}
else
{
Console.WriteLine($"{Name} cannot attack {enemyCreature.Name}.");
Logger.Log($"{Name} cannot attack {enemyCreature.Name}.");
}
}
else
{
Console.WriteLine($"{Name} cannot attack {enemyCreature.Name}.");
Logger.Log($"{Name} cannot attack {enemyCreature.Name}.");
}
}
For example i have a method in this class, is the logging done correctly?
Nergy101
Nergy1012y ago
It's hard to gauge the specifics of your assignment, but usually logging is done through Loggers, and not with Console.Writeline
Protagonist
ProtagonistOP2y ago
below the writeline is a logger
public class Logger
{
private static TraceSource _traceSource = new TraceSource("MyTraceSource");

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information,0,message);
}
}
public class Logger
{
private static TraceSource _traceSource = new TraceSource("MyTraceSource");

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information,0,message);
}
}
the writeline is to write to the screen but im not sure i should have that since this is a framework for a 2D game and there prob wont be a console for it, but this is just to test that things work in my code in a different program
Nergy101
Nergy1012y ago
Ah I see, sorry for that. I would double check if you can't DI a logger from that framework or something like that, or hook into something they use. Otherwise the logging itself seems fine to me
Protagonist
ProtagonistOP2y ago
Would i have to be doing this for all methods, as in adding a logger to log whats happening, so for hit loot combat and so on right, it makes sense I can show the log to the console right? im not sure if this is correct or not but instead of the writelines i can just log it to the console instead of something like a text file right what do u think
Nergy101
Nergy1012y ago
Yeah sounds about right
Protagonist
ProtagonistOP2y ago
static Logger()
{
_traceSource.Listeners.Add(new ConsoleTraceListener());
// Add a file trace listener
_traceSource.Listeners.Add(new TextWriterTraceListener("log.txt"));
}
static Logger()
{
_traceSource.Listeners.Add(new ConsoleTraceListener());
// Add a file trace listener
_traceSource.Listeners.Add(new TextWriterTraceListener("log.txt"));
}
i added this to my logger class but it doesnt write to the console and i cannot find my txt file anywhere once i build in my files
public class Logger
{
private static TraceSource _traceSource = new TraceSource("MyTraceSource");

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information, 0, message);
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="log.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
public class Logger
{
private static TraceSource _traceSource = new TraceSource("MyTraceSource");

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information, 0, message);
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="log.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
this is my logger class and app.config which is in the library and its not working in my console project that im calling it from
Protagonist
ProtagonistOP2y ago
Protagonist
ProtagonistOP2y ago
is this the correct place for the config file to be?
jcotton42
jcotton422y ago
at runtime it should be adjacent to the exe iirc
Protagonist
ProtagonistOP2y ago
whats the exe here if this is a library and not a console app
SUPER MEGA T REX
Libraries shouldn't really have config files The normal thing is for the executable to have a config file
Protagonist
ProtagonistOP2y ago
"The framework must be configurable from a configuration-file" is what i need to do then following this, "The framework must support tracing/logging messages"
SUPER MEGA T REX
I'm not sure about the ins and outs of your assignment but generally in .NET things use the Microsoft logging packages A library usually depends on Microsoft.Extensions.Logging.Abstractions and accepts an ILogger<Whatever> And it's up to the executable to provide it Have you not been given any direction for your assignment?
Protagonist
ProtagonistOP2y ago
the assingment is just making a 2D turn based game framework and it has requirments i dont really understand what it means by "The framework must be configurable from a configuration-file" but i know for tracing it needs to be done right
SUPER MEGA T REX
Have you been working on similar things as part of your course?
Protagonist
ProtagonistOP2y ago
i missed the lesson on tracing/configuration but this is all there is to the content
Protagonist
ProtagonistOP2y ago
Protagonist
ProtagonistOP2y ago
the slides dont mention anything about configuration mostly talks about logging and tracing i think its referring to that then right
SUPER MEGA T REX
I would guess so yeah
MODiX
MODiX2y ago
Protagonist#8031
public class Logger
{
private static TraceSource _traceSource = new TraceSource("MyTraceSource");

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information, 0, message);
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="log.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
public class Logger
{
private static TraceSource _traceSource = new TraceSource("MyTraceSource");

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information, 0, message);
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="log.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
this is my logger class and app.config which is in the library and its not working in my console project that im calling it from
Quoted by
<@!1019262695997968415> from #Assignment Overview help (click here)
React with ❌ to remove this embed.
Protagonist
ProtagonistOP2y ago
it doesnt seem to me displaying on the console
SUPER MEGA T REX
So your framework needs to display trace messages and then the executable can configure them
Protagonist
ProtagonistOP2y ago
im sure the framework would log the trace messages and when framework is being used by consoleapp for example it can then display it to the console but i want to do it from both the console and a txt file neither works tho Listener Default is configured with trace output: System.Diagnostics.DefaultTraceListener it still seems to be using defaultTraceListener so guys I've made the logging work and printing to the console, but whats the configuration file for? also the txt file doesnt seem to log the tracing
public class Logger
{
private static TraceSource _traceSource;
private static int _id;

static Logger()
{
_traceSource = new TraceSource("MyTraceSource");
_traceSource.Switch = new SourceSwitch("MySwitch", "All");

_traceSource.Listeners.Add(new ConsoleTraceListener());
_traceSource.Listeners.Add(new TextWriterTraceListener(new StreamWriter("Log.txt")));

_traceSource.Close();



}

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information, _id, message);
_id++;
_traceSource.Flush();
}
}
public class Logger
{
private static TraceSource _traceSource;
private static int _id;

static Logger()
{
_traceSource = new TraceSource("MyTraceSource");
_traceSource.Switch = new SourceSwitch("MySwitch", "All");

_traceSource.Listeners.Add(new ConsoleTraceListener());
_traceSource.Listeners.Add(new TextWriterTraceListener(new StreamWriter("Log.txt")));

_traceSource.Close();



}

public static void Log(string message)
{
_traceSource.TraceEvent(TraceEventType.Information, _id, message);
_id++;
_traceSource.Flush();
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="Log.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="Log.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
How do i make my library configurable
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server