I am using Cosmos 3.3 sdk and below is the code to write to my container.
public void WriteErrorLogs(Error entity, string collectionName)
{
try
{
Container container = cosmosclient.GetContainer(databaseName, collectionName);
entity.LogID = Guid.NewGuid().ToString();
container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID));
}
catch (Exception ex)
{
throw ex;
}
}
The container in cosmos look like this having partition key as id
the Error class is like this
public class Error
{
[JsonProperty(PropertyName = "id")]
public string LogID { get; set; }
public System.DateTime DateStamp { get; set; }
public string EID { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public string InnerException { get; set; }
I defined partition key to be the LogID which is a guid.The method needs to be synchronous due to the code constraint. Not sure where I am wrong but while debugging always getting" container error CS0103: The name 'container' does not exist in the current context" and logs are not getting created.Any help would be really appretiated,Thanks

You are doing a fire-and-forget, the
CreateItemAsyncis aTask, an async operation, and you are not awaiting it.And anywhere you call
WriteErrorLogsyou need toawaitit too.For example:
During your build process, there might be Warnings pointing at this specific issue.
EDIT: Based on OP comment, adding how to force the operation to be sync, but this is not recommended, you might potentially end with deadlocks, see https://stackoverflow.com/a/24298425/5641598