I’m trying to create a custom log.
I had previously created a dictionary of type <string, string> where all the values were strings.
However, log_index MUST be in int32.
So I changed the dictionary to <string, object>, but since then the program no longer works.
What am I doing wrong?
LogError.zip (163,3 KB)
one thing I observed is the dictionary type is object,object instead of string,object..
apart from that please specify the error you are getting
cheers
oh, sorry i forgot it.
i try even object object but with no success.
The error is present in both cases
Unexpected error has occurred during the library compilation process:
The assembly compilation returned the following errors from ‘Log.xaml’:
- (1660,92): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1660,92): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
- (1666,24): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1686,92): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1686,92): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
- (1692,24): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1712,92): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1712,92): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
- (1718,24): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1764,92): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1764,92): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
- (1770,24): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1790,92): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1790,92): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
- (1796,24): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1816,92): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
- (1816,92): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
- (1822,24): error CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)
use Config<String, object>
but when accessing the values from dictionary,
at that time use .tostring
assume
you have value in config like Age: 24
so here you need to get Age at that scenaio
you need to write Config(“Age”).Tostring() Like this
Happy Automation!!
Like i said, the value cant be a string because the printed log must have this particular value printed like this:
{ “log_index”: 1}
and not like this:
{ “log_index”: “1”}
I need to use a log manager and the first example is required
Here the value is not string, it is in object it can any type but when reteiving the value then you can use .tostring or if you are sure that stored as in the fixed datatype you can you that datatype like .Toint32 or any proper convertors.
I changed in .ToInt32 but the error is still present
LogError.zip (163,3 KB)
Edit: correct errors in project
Trying to understand your requirements here:
- You have a custom log file that has a very specific structure
- The logs are written as json there, with one line = one log entry
- You need to model the structure in a way that after serializing it is exactly as needed (from a proper json perspective)
If above is correct, I’d highly recommend doing a custom class for the structure.
Something like this:
public class LogEntry
{
[JsonProperty("severity")]
public string Severity { get; set; }
[JsonProperty("server")]
public string Server { get; set; }
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("action")]
public string Action { get; set; }
[JsonProperty("timestamp")]
public string Timestamp { get; set; } = DateTime.Now.ToString("yyyyMMdd HH.mm.ss.fff");
[JsonProperty("log_index")]
public int LogIndex { get; set; }
}
And then usage:
LogEntry logEntry = new LogEntry
{
Severity = "high",
Server = "Server01",
Address = "192.168.0.1",
Description = "Test log entry",
Version = "1.0.0",
Action = "Restart",
LogIndex = 42
};
string jsonToWrite = JsonConvert.SerializeObject(logEntry, Formatting.None);
Should be much easier than working with dictionaries.